保存更新后attrubutes无限循环

时间:2016-07-28 10:39:37

标签: node.js loopbackjs

在我的环回应用程序中,我使用mongodb作为我的后端

考虑我有2个收藏品作为A,B。他们的关系是A hasOne B。所以在模型A的aftersave钩子中我实现了

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**:所以当创建A的新实例因为updateAttributes而触发两次时,我如何限制它,就像编辑它应该调用的实例而不是调用updateAttributes ..请分享您的想法。提前谢谢..

1 个答案:

答案 0 :(得分:0)

package pubsub_echo.pubsub;

import com.google.cloud.AuthCredentials;
import com.google.cloud.pubsub.PubSub;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class GCloudPubSub {

    public PubSub getClient() throws FileNotFoundException, IOException {
        PubSubOptions.Builder optionsBuilder = PubSubOptions.builder();
        ClassLoader classLoader = getClass().getClassLoader();
        FileInputStream authStream = new FileInputStream(classLoader.getResource("SERVICE_ACCOUNT.json").getPath());
        AuthCredentials creds = AuthCredentials.createForJson(authStream);

        return optionsBuilder
            .authCredentials(creds)
            .projectId("PROJECT_ID")
            .build()
            .service();
    }

}