Search code examples
javascriptnode.jsloopbackjsstrongloop

Stroongloop, Insert And Update multi Exec


I'm just using loopback last few weeks. I would like to insert in synchronous process. When the first data is inserted into table, then the id will be inserted into the other table.

i've got an error response like this :

{"error":{"status":500,"message":"An unknown error occurred"}}

my code :

   accountModel.create(customerObj.Account, function(error,resp) {
    if(error){
      cb(false, {"message" : "Something Wrong", "err" : error});
    } else {            
      var account_id = resp.result.id;            
      customerObj.Contact.accountId = account_id;
      customerObj.AccountBank.accountId = account_id;
      if(customerObj.Contact){
        contactModel.create(customerObj.Contact, function(err, response) {
          if(err){
            feedbackArr.push({"Contact" : 'error', "err" : err});
          } else {
            feedbackArr.push({"Contact" : 'success', "response" : response});
            if(customerObj.AccountBank){
              bankModel.create(customerObj.AccountBank, function(e,r) {
                if(e){
                  feedbackArr.push({"AccountBank" : 'error', "err" : e});
                } else {
                  feedbackArr.push({"AccountBank" : 'success', "response" : r});
                  cb(true,feedbackArr);
                }
              })
            }
          }
        });
      }
    }
  })

All of models base on PersistedModel. Any ideas ?


Solution

  • If you're using Loopbacks CB-mechanism then I recommend you to pass along the error-object instead of true, "cb(true, feedbackArr)" most likely will result in an unknown error, "cb(error, feedbackArr)" will give you a better feedback.