2015-09-26 17 views
1

我想在從客戶端調用它時從服務器接收的成功回調中寫入數據庫。流星:從服務器方法成功回調時寫入數據庫

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

這是一個路由? 'this.params'從哪裏來? – challett

回答

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 
        } 
       }); 
      } 
     }); 
相關問題