0

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.

dayuloli
  • 16,205
  • 16
  • 71
  • 126

2 Answers2

1

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.

dayuloli
  • 16,205
  • 16
  • 71
  • 126
-1

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

kainlite
  • 1,024
  • 17
  • 35
  • Thank you for your answer, but I thought by defining it in a place accessible on both client and server (it's in `common/methods.js`), then the Meteor method will be ran on the client as a stub. Please correct my understanding. – dayuloli Jun 22 '15 at 14:13
  • Methods as stated in the docs only run in the server, if you want code to run on both the server or the client you've to place it under a folder called lib, or common, but not on client, server, or methods... also you need not to define it as a method if it's intended to run on client and server... – kainlite Jun 22 '15 at 14:15
  • I can recommend you to read this ebook: [Discover Meteor](https://www.discovermeteor.com/) to get you started and so you can understand how everything fits togheter... – kainlite Jun 22 '15 at 14:26
  • Thanks for your help kalnlite, and I have read the relevant part of Discover Meteor. Please help me by taking look at [this answer[(http://stackoverflow.com/a/13145432/2317532) where a stub is defined on the client using `Meteor.methods`. – dayuloli Jun 22 '15 at 14:29
  • I see your point, that's part of latency compensation, so your changes will be made immediately on the client, but it will only be final or effective when the servers returns from the method call, is that clear? – kainlite Jun 22 '15 at 14:31
  • Yes I understand latency compensation, so why is the `console.log()` not printing anything. I expect that to be printed immediately. Even when I make the server-side operation take a while by using a big loop, the client-side stub is still not running. – dayuloli Jun 22 '15 at 14:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81191/discussion-between-kainlite-and-dayuloli). – kainlite Jun 22 '15 at 14:35