后保存更新attrubutes无限循环
问题描述:
在我回送的应用程序,我使用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 ..请分享你的想法。在此先感谢..
答
if(isNewInstance) {
if(ctx.instance.needToUpdate){
process.nextTick(() => {
ctx.instance.updateAttributes();
});
}
} else {
}
+0
这会触发更新实例A时也会右键? – Subburaj
+0
@Subburaj是的但只有一次 –
在save'hook期间,你想在'A'和'B'上做什么? – Overdrivr
@Overdrivr谢谢..在afterSave A我计算一个JSON并在B(其中_id)中创建一个文档。我必须将_id更新为A中的一个键.. – Subburaj
所以基本上,创建一个'B'的实例并将其链接到'A'的当前实例。它是否正确 ? – Overdrivr