簡體   English   中英

使用“findOne”或“findAll”后,Sequelize 不更新

[英]Sequelize doesn't update after using 'findOne' or 'findAll'

我遇到了這個問題,我嘗試從 dB 更新找到的行。 我為這個 v5 使用了 sequelize。 問題是在找到我想要的行之后,我使用了很多語法,但都沒有奏效。 我嘗試了 instance.update({thing: value})、instance.save({thing: value})、model.update 等。它不會返回任何類型的錯誤或警告,但不會更新任何內容。

const t = await sequelize.transaction();
try{
    const foundUser = await User.User.findOne({
        where: {
            userName: req.body.userName,
            password: req.body.password
        }
    }, {transaction: t });
    await userFunction.validateLogin(foundUser);
    foundUser.tooken = '10:10:10';
    await foundUser.save();
    await t.commit();

這里只是返回找到的行,它不會更新任何內容。 這是返回的內容:

Executing (60652c1d-4e58-449e-bde1-3ae7512651ac): START TRANSACTION;
Executing (default): SELECT "id", "firstName", "lastName", "userName", "password", "email", "birthDay", "createdAt", "updatedAt" FROM "users" AS "user" WHERE "user"."userName" = 'dudu3000' AND "user"."password" = 'admin' LIMIT 1 FOR UPDATE;
Executing (60652c1d-4e58-449e-bde1-3ae7512651ac): COMMIT;

您可以在findOne方法的.then中調用 update 方法

SequlizeModel.findOne({
      where: {
            userName: req.body.userName,
            password: req.body.password
        }
})
.then(record => {

  if (!record) {
    throw new Error('No record found')
  }

  console.log(`retrieved record ${JSON.stringify(record,null,2)}`) 

  record.update(values).then( updatedRecord => {
    console.log(`updated record ${JSON.stringify(updatedRecord,null,2)}`)
    // login into your DB and confirm update
  })

})
.catch((error) => {
  // do seomthing with the error
  throw new Error(error)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM