Knex事务无法工作,因为无法读取未定义错误的属性“ then”

时间:2019-01-28 20:54:18

标签: javascript node.js express promise knex.js

我正在Node Express上使用Knex js进行事务。事务涉及插入两个表。为了进行测试,我对列名称及其值进行了硬编码。

db('table_1').where('id', 1) 
    .then(data => {
      if (data.length === 0) {
        db.transaction(trx => {
          db.raw(`insert into table_1 (id, f_name) values ('1', 'hello');`).transacting(trx)   
              console.log('works 1')

              .then(data => {

                return db.raw(`insert into table_2 (id, l_name) values ('1', 'kitty');`).transacting(trx)    
                  .then(data => {
                  console.log(data[0])
                })
            })
            .then(trx.commit)
            .catch(trx.rollback);
        })
          .then(() => res.sendStatus(200))
          .catch(err => console.log(err));
      }
    })

第一次插入有效,因为console.log有效,但是随后出现错误Cannot read property 'then' of undefined。我读到,当缺少then时会发生此错误,但是在这里看不到问题。

任何帮助将不胜感激。

0 个答案:

没有答案