我想在从客户端调用服务器时从服务器收到的成功回调中写入数据库。
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
我预计它只会在服务器调用成功时更新数据库。我怎样才能做到这一点?
答案 0 :(得分:1)
假设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
}
});
}
});