Sequelize - update record, and return result

后端 未结 7 881
粉色の甜心
粉色の甜心 2020-12-28 11:20

I am using sequelize with MySQL. For example if I do:

models.People.update({OwnerId: peopleInfo.newuser},
        {where: {id: peopleInfo.scenario.id}})
             


        
7条回答
  •  庸人自扰
    2020-12-28 12:02

    There is another way - use findByPk static method and update not-static method together. For example:

    let person = await models.People.findByPk(peopleInfo.scenario.id);
    if (!person) {
      // Here you can handle the case when a person is not found
      // For example, I return a "Not Found" message and a 404 status code
    }
    person = await person.update({ OwnerId: peopleInfo.newuser });
    response(person).code(200);
    

    Note this code must be inside an asynchronous function.

提交回复
热议问题