Why is console.log() not working client-side when inside Meteor methods?

2.3k views Asked by At

I want to define the same method for both client and server, so I have the following code inside common/methods.js.

Meteor.methods({
    doSomething: function (obj) {
        if(this.isSimulation) {
            console.log('client');
        }
        if(!this.isSimulation) {
            console.log('server');
        }
        console.log('both');
        return "some string";
    }
});

Then I called this method inside client/dev.js.

Meteor.call('doSomething', someObj, function (e, data) {
    console.log(e);
    console.log(data);
});

On the server's console, I can read:

I20150622-21:56:40.460(8)? server
I20150622-21:56:40.461(8)? both

On the client's (Chrome for Ubuntu v43.0.2357.125 (64-bit)) console, the e and data arguments are printed, but the console.log() from the Meteor method is not, where I expected it to output the strings

client
both

Why do console.log() not work on the client inside Meteor methods?


To debug, I split the Meteor.methods into separate client and server code. Then introducing a large loop so the server-side operation so it takes a long time to complete, while the client-side is very quick.

server

doSomething: function (obj) {
    var x = 0;
    for(var i = 0; i< 9999999; i++) {
        x++;
    }
    console.log(x);
    return "some string";
}

client

doSomething: function (obj) {
    console.log('client');
}

Still, no message is printed on the client.

2

There are 2 answers

0
dayuloli On BEST ANSWER

Thanks to @kainlite for helping me debug this together. It turns out the problem was a simple one of file load order.

I defined my methods in common/methods.js, whereas my client-side calls were made in client/dev.js, which gets loaded first.

So when the call was made the method wasn't defined, hence it won't run. Moving the methods.js file inside the /lib directory fixed the issue.

7
kainlite On

Methods are only executed on the server, they are the sync way of doing a remote call.

Methods

Methods are server functions that can be called from the client. They are useful in situations where you want to do something more complicated than insert, update or remove, or when you need to do data validation that is difficult to achieve with just allow and deny.

http://docs.meteor.com/#/basic/Meteor-users