我在客户端有这段代码:
var Checklist = {
title: this.title,
belongs_to: this.belongs_to,
type: this.type,
items: this.items
};
Meteor.call(
'create_checklist',
Checklist,
function(error,result){
console.log('error',error,'result',result);
// if(!error) {
// Router.go('/checklist/'+response);
// }
}
);
服务器上的这个:
create_checklist: function(Checklist) {
Checklists.insert(
{
title: Checklist.title,
belongs_to: Checklist.belongs_to,
type: Checklist.type,
items: Checklist.items
}, function(error,id){
console.log(error,id);
if(id) {
return id;
} else {
return error;
}
}
);
},
Meteor.call 成功地将信息传递给服务器,因为 list 已创建。我可以在服务器控制台中看到新 list 的 ID。但是,对于错误和结果,客户端只会看到 undefined
。
最佳答案
您不会在服务器方法中返回结果。您不能从回调中返回值。仅返回 Checklists.insert 的结果:
create_checklist: function(Checklist) {
return Checklists.insert(
{
title: Checklist.title,
belongs_to: Checklist.belongs_to,
type: Checklist.type,
items: Checklist.items
}, function(error,id){
console.log(error,id);
if(id) {
return id;
} else {
return error;
}
}
);
},
根据 Meteor docs , insert 方法返回插入文档的 ID。
在服务器上,如果您不提供回调,则插入 block 直到数据库确认写入,或者如果出现问题则抛出异常。
关于javascript - Meteor.call 回调函数返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838183/