我试图首先检查表中的值,如果它存在,则删除另一个表中的一行并将此新数据插入该表中。
我使用了一个带有 select、del() 和 insert 命令的事务
db.transaction(trx => {
return trx('users')
.where({ username: user.username })
.select('username')
.returning('username')
.then(retData => {
retUserName = retData[0];
db('profile')
.where({ username: user.username })
.del()
.then(retData => {
return trx
.insert(profileData)
.into('profile')
.returning('*');
});
})
.then(retData => {
res.json({ ProfileData: profileData });
})
.then(trx.commit)
.catch(trx.rollback);
}).catch(err => res.status(400).json('unable to create profile'));
Run Code Online (Sandbox Code Playgroud)
我收到此错误未处理的拒绝错误:事务查询已完成
但数据尚未添加到表中。