Search code examples
javascriptnode.jsloopbackjsstronglooploopback

callback was already called! in loopback, in updateAll function


I'm using the loopback, here while making the update call with list of objects in array.

I get in the callback is already called!

The scene is, I have defined the callback inside the loop, and in the first loop, it is get in called actually.

I am looking for the way where

I should update all list of object in query MySQL plan call.

    Inward.updateIsActiveDetails = function(data, callback) {
        var id = _.map(data, 'id');
        if (id.length > 0) {
          _.forEach(id, id => {
            console.log('id....:', id)
            Inward.updateAll({id}, {
              isActive: 0,
            }).then(updateresult => {
              console.log(updateresult);
   // callback(error); showing err with it... (callback already called)
            }).catch(function(error) {
              callback(error);
            });
          });
        } else {
          callback(null, {
            success: true,
            msg: 'No records to update',
          });
        }
      };

output:

id....: 3
id....: 4
{ count: 1 }
{ count: 1 }

appreciate for right solution


Solution

  • Here is my final and working answer.

    Basically, updateAll query runs once and it will run as an inbuilt query

      id: {
            inq: _.map(data, 'id'),
          }
    

    So, after running that it will update the respective row only! very interesting.

     Inward.updateIsActiveDetails = function (data, callback) {
        Inward.updateAll({
          id: {
            inq: _.map(data, 'id'),
          },
        }, {
            isActive: 0,
          }, function (error, resultDetails) {
            if (error) {
              console.log('error', error);
              callback(error);
            } else {
              console.log('resultDetails', resultDetails);
              callback(null, resultDetails);
            }
          });
      };