1

I'm trying to return a MongoDB cursor from a MeteorJS server side method. I can return the array to the server side but can't figure out how to pass it back into the client. What's the best way to do this?

//current server side
if (Meteor.isServer) {
    Meteor.methods({
        'mongo.updateSearchQuery' (searchQuery) {
            var queryCursor = remoteEvents.find({
                $text: {
                    $search: searchQuery
                }
            }).fetch()
            console.log(queryCursor);
            return (
                queryCursor
            )
        }
    });
}

//current client side
callMongoTextSearch() {
    var searchQuery = this.state.searchQuery;
    var searchQuery = Meteor.call('mongo.updateSearchQuery', searchQuery);
    console.log(searchQuery);
}
dakab
  • 5,379
  • 9
  • 43
  • 67
ElJefeJames
  • 295
  • 3
  • 16

1 Answers1

2

You have to use a callback as the last argument of your Meteor.call in order to use the returned result from your server Meteor.methods. Whatever you return in your Meteor.methods will be passed as the 2nd argument of your callback.

For example:

Meteor.call('mongo.updateSearchQuery',searchQuery, function (error, result) {
  console.log(result); // result will be your `queryCursor`
});

You should probably also make sure you get the concept of asynchronous tasks, e.g. How do I return the response from an asynchronous call?

ghybs
  • 47,565
  • 6
  • 74
  • 99