无法解决Node.js

时间:2018-04-13 07:07:25

标签: javascript ecmascript-6 promise es6-promise ecmascript-5

我已经创建了一个实际上有很多异步调用的函数

功能是这样的

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

我在另一个函数中调用此函数,在此函数调用之后我正在更新另一个表中的数据 这个功能

目前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 } ]

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

我真的被这个承诺链所困扰,无法理解我该怎么办。请提供一些提示

1 个答案:

答案 0 :(得分:0)

在任何承诺链中,每个可用的块必须返回数据或另一个承诺。这是承诺链的拇指规则。

函数createOrUpdatePlan在“if”块中返回一个promise,因此它也会在“else”块中返回一个promise。你正在返回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
   })