MeteorJS: Get success callback from user update?

一笑奈何 提交于 2019-12-25 07:47:03

问题


So, I just want to know if my user update was successful on client, so I can notify the user that the update worked.

//Client Side
return Meteor.call('handleUpdateUser', _id, _username, function(err, res) {
  if (err) {
    // also, what is best practices for handling these errors?
  }
  console.log(res);
});

//Server Side
Meteor.methods({
  handleUpdateUser(id, username) {
    if (check for user...) {
      return if found
    }
    return Meteor.users.update({_id: id}, {$set: {username: username}}, 
    function(err, count, res) {
      if (err) {
        console.log(err) // again, best practices for handling these errors?
      }
      return res;
    });
 }

I currently get undefined in console on client.

permissions.js:

//Server Side
Meteor.users.allow({
  update:function(userId, doc, fields, modifier) {
    return userId && doc._id === userId;
  }
});

Thoughts?


回答1:


you're getting undefined because your server side method is async and, absent treating it as such, your client will see the result of the synchronous part. i.e. the implicit undefined returned at the end of handleUserUpdate().

i use a Future to treat it as async. e.g.

const Future = Npm.require('fibers/future');

Meteor.methods({
    handleUpdateUser(id, username) {
        let future = new Future();

        Meteor.users.update({_id: id}, {$set: {username: username}}, function(err, res) {
            if (err) {
                console.log(err);
                future.throw(new Meteor.Error('500', err));
            }

            future.return(res);
        });

        return future.wait();
    }
});

now, Meteor will wait to notify the client until the future is handled, either through a return or a throw. your client call should work as you expect.




回答2:


From the docs: https://docs.meteor.com/api/collections.html#Mongo-Collection-update

On the server, if you don’t provide a callback, then update blocks until the database acknowledges the write, or throws an exception if something went wrong. If you do provide a callback, update returns immediately. Once the update completes, the callback is called with a single error argument in the case of failure, or a second argument indicating the number of affected documents if the update was successful.

So if you want to wait for result, you should think of update as of a sync operation to avoid all those Futures:

Meteor.methods({
    handleUpdateUser(id, username) {
        return Meteor.users.update({ _id: id }, { $set: { username: username } });
    }
});

If operation fails, it will throw an error and pass it on to the client. If it succeeds, then it will return the result.

If you want to check what was the error (on server side), then wrap it into a regular try {} catch(e) {}.



来源:https://stackoverflow.com/questions/43564217/meteorjs-get-success-callback-from-user-update

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!