Abzal Serekov
Abzal Serekov

Reputation: 127

Why asynchronous Meteor.call() doesn't return anything

I have the following code on the client-side:

Meteor.call("getOldTests", m_user, test_id, course, language, function (error, result) {
            console.log("result: " + result);
            if (error) {
                Notifications.error(error.reason, 'We are working on this problem');
            } else {
                console.log(result);
                data.set(course, result);
            }

        });

where the server-side has the following method:

Meteor.methods({
    getOldTests: function (m_user, test_id, course, language) {
        var tests = Tests.findOne({email: m_user.email, course_en: course, test_id: test_id});
        if (tests) {
            console.log(tests);
            return Questions.find({course_en: course, variant: tests.variant, language: language});
        } else {
            return false;
        }
    },});

Where the variable data is reactive-dict

So, why is nothing executed inside of my Meteor.call() function on the client-side (no console output) while indeed it calls the method on the server-side (console outputs intermediate results)?

Thanks,

Upvotes: 0

Views: 61

Answers (2)

Michel Floyd
Michel Floyd

Reputation: 20256

It looks like you are using a method where a pub-sub is more appropriate:

server:

Meteor.publish('getOldTests',function (m_user, test_id, course, language) {
  var tests = Tests.findOne({email: m_user.email, course_en: course, test_id: test_id});
  if (tests) return Questions.find({course_en: course, variant: tests.variant, language: language});
  else this.ready();
});

client:

Meteor.subscribe('getOldTests',m_user, test_id, course, language);

Upvotes: 1

Abhijith Sasikumar
Abhijith Sasikumar

Reputation: 15100

Database fetch and API calls form server side should be contained inside a Fiber. Try this:

Future = Npm.require('fibers/future');
getOldTests: function (m_user, test_id, course, language) {
    var myFuture = new Future();
    var tests = Tests.findOne({email: m_user.email, course_en: course, test_id: test_id});
    if (tests) {
        console.log(tests);
        myFuture.return(Questions.find({course_en: course, variant: tests.variant, language: language}));
    } else {
        return false;
    }
 return myFuture.wait();
}

This question might be helpful.

Upvotes: 0

Related Questions