Sequelize - update record, and return result

后端 未结 7 878
粉色の甜心
粉色の甜心 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:18

    same thing you can do with async-await, especially to avoid nested Promises You just need to create async function :)

    const asyncFunction = async function(req, res) {
        try {
            //update 
            const updatePeople = await models.People.update({OwnerId: peopleInfo.newuser},
                                        {where: {id: peopleInfo.scenario.id}})
            if (!updatePeople) throw ('Error while Updating');
            // fetch updated data
            const returnUpdatedPerson =  await models.People.findById(peopleInfo.scenario.id)
            if(!returnUpdatedPerson) throw ('Error while Fetching Data');
            res(user).code(200);
        } catch (error) {
            res.send(error)
        }
    } 
    

提交回复
热议问题