无法解决 Node.js 中的承诺链错误

Not able to resolve promise chain error in Node.js

我做了一个函数它实际上有很多异步调用

函数是这样的

const createOrUpdatePlan = (billPlans, serviceId, update) => {
  let billPlansWithId;
  let promises = [];
  if (!update) {
    billPlans.map(bp => {
      bp.serviceId = serviceId;
      return bp;
    });
    console.log(billPlans);
    return db.serviceBillPlans.bulkCreate(billPlans);
  } else {

     //first promise
    let findPromise = db.coachingClasses
      .findAll({
        attributes: ['billPlans'],
        where: {
          id: serviceId
        }
      })
      .then(previousBillPlans => {
        //creating new bill plans in edit class
        let newBillPlans = billPlans.filter(bp => !bp.id);

        if (newBillPlans.length > 0) {
          newBillPlans = newBillPlans.map(bp => {
            bp.serviceId = serviceId;
            return bp;
          });
          // console.log(newBillPlans);

           //second promise 
          let createPromise = db.serviceBillPlans
            .bulkCreate(newBillPlans)
            .then(newPlans => {
              let p1;
              billPlansWithId = billPlans.filter(bp => bp.id);
              if (newPlans) {
                newPlans.forEach(element => {
                  let object = {};
                  object.id = element.id;
                  (object.name = element.name),
                    (object.cycle = element.cycle),
                    (object.fees = element.fees);
                  billPlansWithId.push(object);
                });

              }
              console.log(billPlansWithId);
              billPlans = billPlansWithId;
              return billPlans;
            });
          promises.push(createPromise);
        }
      });
    promises.push(findPromise);
    return Promise.all(promises).then((arr) => arr[1] );
  }
};

我在另一个函数中调用这个函数,在这个函数调用之后我正在更新另一个 table 中的数据,该数据由 这个函数

目前 createOrUpdatePlan 函数中发生的第一个承诺是 运行 但之后是我插入数据的第二个承诺,然后是 then

 .then(newPlans => {
          let p1;
          billPlansWithId = billPlans.filter(bp => bp.id);
          if (newPlans) {
            newPlans.forEach(element => {
              let object = {};
              object.id = element.id;
              (object.name = element.name),
                (object.cycle = element.cycle),
                (object.fees = element.fees);
              billPlansWithId.push(object);
            });

          }
          console.log(billPlansWithId);
          billPlans = billPlansWithId;
          return billPlans;
        });

then块代码是运行此函数在另一个函数

中返回数据后

因为我在这个 then 块中写了 console.log 所以我得到的日志是这样的

INSERT INTO `service_bill_plans` (`id`,`service_id`,`name`,`cycle`,`fees`,`created_at`,`updated_at`) VALUES (NULL,'17','Five Months Plan',5,4000,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP),(NULL,'17','Six Months Plan',6,5000,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
undefined
(node:8412) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SequelizeValidationError: notNull Violation: coachingClasses.billPlans cannot be null
(node:8412) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[ { id: 1, name: 'Monthly Plan', cycle: 1, fees: 1000 },
  { id: 2, name: 'Yearly Plan', cycle: 12, fees: 10000 },
  { id: 3, name: 'Two Months Plan', cycle: 2, fees: 1500 },
  { id: 4, name: 'Three Months Plan', cycle: 3, fees: 2500 },
  { id: 5, name: 'Four Months Plan', cycle: 4, fees: 3000 },
  { id: 148, name: 'Five Months Plan', cycle: 5, fees: 4000 },
  { id: 149, name: 'Six Months Plan', cycle: 6, fees: 5000 } ]

因为您可以看到数据已插入到此函数的 table 中,但之后在 then 块中,它不会在此函数中返回数据之前返回。

我真的被这个 promise 链困住了,无法理解我应该做什么。请给点提示

在任何 promise 链中,每个 then'able 块都必须 return 一个数据或另一个 promise。这是 promise 链的经验法则。

函数 createOrUpdatePlan 是 return 在 "if" 块中的一个承诺,因此它也应该是 return 在 "else" 块中的一个承诺。您 returning Promise.all 是正确的,但您在 Promise.all

中结合了内在承诺和外在承诺
db.coachingClasses    // findPromise is created here (main).
  .findAll({ ... })
  .then(previousBillPlans => {
     // createPromise is created here (inner promise); 

     // This then'able block must have a return data/promise - missing
   })

return Promises.all(); // mix of inner and outer makes no sense.

预期的承诺链如下

return db.coachingClasses    // findPromise
  .findAll({ ... })
  .then(previousBillPlans => {
     // return createPromise
   })