流星插入方法回调应该执行异步
Meteor insert method callback should execute async
你好 Whosebug 社区,
疯狂
我想知道为什么插入回调没有像文档中所说的那样被正确调用异步,代码如下:
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
之后执行,如果函数是异步的,回调函数中的调试器应该在最后调用,对吗?
更多信息
我对 meteor 很陌生,我正在尝试制作一个小应用程序并进行试验,现在我想确认我对这种情况下的一些概念的理解 "insert"方法。给定以下代码:
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);
之类的操作并且一切顺利时,会显示一条成功消息,否则会出现 "truth" 并显示一条错误消息。实际发生的是回调总是被调用并带有未定义的错误(就像一切都很酷),回调也没有被称为异步,它只是被直接调用。
即使我关闭连接,更新插入也会显示一条成功消息。
有什么想法吗?也许我的应用程序结构让 meteor 工作出错了?我真的不知道。提前致谢。
您的代码在方法内部执行。在客户端上,执行方法只是为了模拟服务器在服务器响应之前将执行的操作(以便应用程序看起来响应更快)。因为这里的 DB 变化只是模拟服务器已经在做的事情,它们并没有发送到服务器,因此是同步的。在服务器上,所有代码 运行 都在 Fiber 中,因此它是同步的。 (但是,Fibers 运行 像普通的回调汤节点一样并行。)
你好 Whosebug 社区,
疯狂
我想知道为什么插入回调没有像文档中所说的那样被正确调用异步,代码如下:
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
之后执行,如果函数是异步的,回调函数中的调试器应该在最后调用,对吗?
更多信息
我对 meteor 很陌生,我正在尝试制作一个小应用程序并进行试验,现在我想确认我对这种情况下的一些概念的理解 "insert"方法。给定以下代码:
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);
之类的操作并且一切顺利时,会显示一条成功消息,否则会出现 "truth" 并显示一条错误消息。实际发生的是回调总是被调用并带有未定义的错误(就像一切都很酷),回调也没有被称为异步,它只是被直接调用。
即使我关闭连接,更新插入也会显示一条成功消息。
有什么想法吗?也许我的应用程序结构让 meteor 工作出错了?我真的不知道。提前致谢。
您的代码在方法内部执行。在客户端上,执行方法只是为了模拟服务器在服务器响应之前将执行的操作(以便应用程序看起来响应更快)。因为这里的 DB 变化只是模拟服务器已经在做的事情,它们并没有发送到服务器,因此是同步的。在服务器上,所有代码 运行 都在 Fiber 中,因此它是同步的。 (但是,Fibers 运行 像普通的回调汤节点一样并行。)