当我从客户端调用它时,我只想在从服务器成功收到回调时写入数据库。
Meteor.call('job', 'new', name, script, function(err,response) {
if(err) {
console.log(err);
alert('Error while processing your script. Please make sure syntax is correct.')
return;
}else{
taskid = response;
console.log(taskid);
FileSystem.update({ _id: this.params.fileId }, { $set: { content: content, taskid:taskid} }, function (e, t) {
if (e) {
//error
}
});
}
});
现在写它说
Exception in delivering result of invoking 'job': TypeError: Cannot read property 'fileId' of undefined
我预计它只会在服务器调用成功时更新数据库。我怎样才能做到这一点?
最佳答案
假设 this.params 存在,您可能会丢失这些回调函数内的数据上下文。您想要做的是在 Meteor.call()
之前定义一个变量,并将该变量设置为 this.params.fileId
。然后您可以在回调函数内使用该变量。
我已在下面的代码中展示了这一点。
var fileId = this.params.fileId;
Meteor.call('job', 'new', name, script, function(err,response) {
if(err) {
console.log(err);
alert('Error while processing your script. Please make sure syntax is correct.')
return;
}else{
taskid = response;
console.log(taskid);
FileSystem.update({ _id: fileId }, { $set: { content: content, taskid:taskid} }, function (e, t) {
if (e) {
//error
}
});
}
});
关于javascript - Meteor:从服务器方法成功回调后写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32801694/