2016-04-23 102 views
0

後,我想插入後寫入到另一個集合,併爲該任務,我使用流星蒐集鉤子包裝https://github.com/matb33/meteor-collection-hooks使用流星收集鉤插入

我想知道我將如何插入火災後回調。 Currenty我使用這個代碼中插入

'schoolNew': function(post){ 

     Schools.insert({ 
      schoolname: post.input_sn, 
      schooldescription: post.input_sd, 
      schoollocation: post.input_sl, 
      schoollogo: post.input_ls 
     }); 
}, 

正如醫生說https://github.com/matb33/meteor-collection-hooks#afterinsertuserid-doc

的文檔插入後激發。

我該如何使用回調?

+0

插入返回該ID後,您可以獲取與該ID的數據。 –

回答

2

你剛纔定義集合鉤,像這樣的例子:

Schools.after.insert(function(userId, doc) { 
    console.log(this); 
    console.log(userId, doc); 
}); 

,它會自動每隔後(!)插入到Schools收集調用。

+0

聽起來不可思議,但它與我擁有的每一個其他包裹混雜在一起https://github.com/vsivsi/meteor-job-collection/issues/58#issuecomment-72272402 –

+1

我知道這很糟糕!在定義'新的JobCollection'之前試試這個:'Mongo.Collection.prototype.constructor = Mongo.Collection'。 – tomsp

0

我加入了鉤這樣

'schoolNew': function(post){ 

     Schools.insert({ 
      schoolname: post.input_sn, 
      schooldescription: post.input_sd, 
      schoollocation: post.input_sl, 
      schoollogo: post.input_ls 
     }); 
     Schools.after.insert(function (userId, doc) { 
     var newid = this._id; 
     console.log(newid); 
    }); 
}, 

我雖然沒有遇到問題,但是這條評論將幫助您https://github.com/vsivsi/meteor-job-collection/issues/58#issuecomment-72272402

+0

'Schools.after.insert'不會進入'方法'。 – tomsp