2016-07-28 32 views
1

在我回送的應用程序,我使用MongoDB的作爲我的後端後保存更新attrubutes無限循環

考慮我有2個集合爲A,B。他們的關係是A hasOne B。所以在我的aftersave鉤模型A的我已經實現

if(isNewInstance) { 
    // When creating A i have to compute data 
    // and create a document in B and have to update the _id of B to A  
    // For Updating i am calling:  
    ctx.instances.updateAttributes();// this will once again call this 
    //after save hook and with isNewInstance == false, 
    // so it will go in else condition also. 
} else { 
    // When Updating A i have to compute data and create a document in B 
    // and have to update the _id of B to A 
} 

**Summary**:所以創建的新實例時觸發,因爲updateAttributes的兩倍,我怎麼能限制編輯實例時像它應該叫,但不在調用updateAttributes ..請分享你的想法。在此先感謝..

+0

在save'hook期間,你想在'A'和'B'上做什麼? – Overdrivr

+0

@Overdrivr謝謝..在afterSave A我計算一個JSON並在B(其中_id)中創建一個文檔。我必須將_id更新爲A中的一個鍵.. – Subburaj

+0

所以基本上,創建一個'B'的實例並將其鏈接到'A'的當前實例。它是否正確 ? – Overdrivr

回答

0
if(isNewInstance) { 
    if(ctx.instance.needToUpdate){ 
    process.nextTick(() => { 
     ctx.instance.updateAttributes(); 
    }); 
} 
} else { 

} 
+0

這會觸發更新實例A時也會右鍵? – Subburaj

+0

@Subburaj是的但只有一次 –