【问题标题】:callback was already called! in loopback, in updateAll function回调已被调用!在环回中,在 updateAll 函数中
【发布时间】:2019-05-01 15:48:13
【问题描述】:

我在这里使用环回,同时使用数组中的对象列表进行更新调用。

我得到的回调已经被调用了!

场景是,我已经在循环内部定义了回调,在第一个循环中,它实际上是被调用的。

我在找路

我应该更新查询 MySQL 计划调用中的所有对象列表。

    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',
          });
        }
      };

输出:

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

欣赏正确的解决方案

【问题讨论】:

    标签: javascript node.js loopbackjs strongloop loopback


    【解决方案1】:

    回调应该被调用一次,你是在循环中调用它,所以循环的每次迭代都会调用它。不止一次。如果出于某种原因您不能使用 async/await,以下内容将是正确的。

    Inward.updateIsActiveDetails = function(data, callback) {
        var id = _.map(data, 'id');
        var len = id.length;
        var resultList = [];
    
        // When you call this function we add the results to our list
        // If the list of updates is equal to the number of updates we had to perform, call the callback.
        function updateResultList(updateResult) {
          resultList.push(updateResult);
          if (resultList.length === len) callback(resultList);
        }
        if (len > 0) {
          _.forEach(id, id => {
            Inward.updateAll({id}, {
              isActive: 0,
            })
            .then(updateResult);
          });
        } else {
          callback(null, {
            success: true,
            msg: 'No records to update',
          });
        }
      };
    

    使用 async/await 会更短。

    Inward.updateIsActiveDetails = async function(data) {
      const results = [];
      for(let i = 0; i < data.length; i++) {
        results.push(await Inward.updateById(data[i].id));
      }
      return results;
    }
    

    【讨论】:

      【解决方案2】:

      这是我的最终答案。

      基本上,updateAll 查询运行一次,它将作为内置查询运行

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

      所以,运行后它只会更新相应的行!很有趣。

       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);
              }
            });
        };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 2018-01-11
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 2018-10-20
        相关资源
        最近更新 更多