在服务器端我有方法:
Meteor.methods({
'group.new'({ name, desc }) {
if (!Meteor.userId()) {
throw new Meteor.Error('You need to be logged in to comment');
}
Groups.insert({
name,
desc,
createdBy: Meteor.userId(),
created: new Date(),
});
},
});
我从前端的React组件调用它:
Meteor.call(
'group.new',
{
name: this.state.name,
desc: this.state.desc,
},
function(err, res) {
console.log(err);
console.log(res);
},
);
为什么err和res总是未定义?
答案 0 :(得分:0)
您似乎没有从服务器返回任何内容。这就是为什么当插入成功时,客户端中的resp
对象仍未定义。但是,错误对象应该可以工作。也许只是你没有遇到错误的情况。
这将有效:
<强> server.js 强>
Meteor.methods({
'group.new'({ name, desc }) {
if (!Meteor.userId()) {
throw new Meteor.Error('You need to be logged in to comment');
}
// if insert succeeds, it will return the _id of the doc.
var result_id = Groups.insert({
name,
desc,
createdBy: Meteor.userId(),
created: new Date(),
});
return result_id; // send the id back to the client
},
});
现在,如果客户端上有console.log(res)
,您应该能够查看从服务器发回的插入文档的_id。