简体   繁体   中英

Meteor insert method callback should execute async

Hello Stackoverflow community,

In nuts

I am wondering why the insert callback is not being called async properly as the documentation says, having a code like:

Meteor.methods({
  addUpdate: function (text) {
    Updates.insert({
      text: text,
      createdAt: new Date(),
      owner_email: Meteor.user().emails[0].address,
      owner_username: Meteor.user().username
    }, function(e, id) {
      debugger; //<-- executed first with 'e' always undefined
    });
    debugger; //<-- executed after
  }
});

the debugger inside the callback function is executed before the debugger afterwards, if the function is async the debugger inside the callback should be called at the end right?

More info

I am very new with meteor, the thing is that I am trying to make an small app, and experimenting, by now I wanted to confirm what I had understood about some concepts in this case the "insert" method. given the following code:

lib/collections/updateCollection.js

Update = function (params, id) {
  params = params || {};
  // define properties for update model such as text
  this._text = params.text;
}

Update.prototype = {
  // define some getters and setters, such as doc
  get doc() {
    return {
      createdAt: this.createdAt,
      text: this.text,
      owner_email: this.owner_email,
      owner_username: this.owner_username
    };
  },
  notify: function notify(error, id) {
    var client, notification, status;

    client = Meteor.isClient ? window.Website : false;
    notification = (client && window.Hub.update.addUpdate) || {}
    status = (!error && notification.success) || notification.error;

    if (client) {
      return client.notify(status);
    }
  }
  save: function save(callback) {
    var that;

    that = this;
    callback = callback || this.notify;

    Updates.insert(that.doc, function (error, _id) {
      that._id = _id;
      callback(error, _id); <-- here is the deal
    });
  }
}

lib/methods/updateService.js

updateService = {
  add: function add(text) {
    var update;

    update = new Update({
      text: text,
      createdAt: new Date(),
      owner_email: Meteor.user().emails[0].address,
      owner_username: Meteor.user().username
    });

    update.save();
  },

  // methods to interact with the Update object
};

lib/methods/main/methods.js

Meteor.methods({
  addUpdate: function (text) {
    updateService.add(text);
  }
});

My expectations here is when the client do something like Meteor.call('addUpdate', text); and everything is cool, a successful message is shown, otherwise the error is "truth" and an error message is shown. What is actually happening is that the callback is always called with error undefined (like if everything es cool), the callback also is not being called async, it is just called directly.

Even when I turn off the connection the update insertion shows a success message.

Any idea? maybe my app structure is making meteor work wrong? I really do not know. Thanks in advance.

Your code is executing inside a method. On the client, methods are executed simply to simulate what the server will do before the server responds (so that the app seems more responsive). Because the DB changes here are just simulating what the server is already doing, they are not sent to the server, and therefore synchronous. On the server, all code runs inside a Fiber , so it acts synchronous. (However, Fibers run in parallel just like normal callback-soup Node.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM