你好#1社区,流星插入方法回调应该执行异步
在坚果
我很奇怪,为什么插入回调不被称为异步正确的文件说,有像代码:
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
}
});
回调函数内debugger
是debugger
之后才执行,如果函数是异步回调SH内调试器应该被称为最后的权利?
更多信息
我很新与流星的事情是,我试图做一个小的应用程序,并尝试,现在我想证实我的理解有关此的一些概念case“插入”方法。给出以下代码:
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);
}
});
我在这里的期望是当客户端做一些像Meteor.call('addUpdate', text);
,一切都很酷,显示了一个成功的消息,否则错误是“真理”,并显示错误信息。实际发生的事情是,回调总是被调用,并且未定义错误(如果一切都很酷),回调也不会被称为异步,它只是直接调用。
即使关闭连接,更新插入也会显示成功消息。
有什么想法?也许我的应用程序结构让流星工作错了?我真的不知道。提前致谢。
where.doc'属性被填充到哪里? – Curtis
在Update.property中,我只是跳过它来使代码更小,是一个getter。然后我要更新代码。 –