简体   繁体   中英

Update database using Sequelize for existing field given in request Node.Js

How to update our model only for existing field given by client to our API?

Example:

User.update({‘name’: ‘value_1’, {‘email’: ‘value_2’,}.then().catch()

I want to automatically update my User model based on what my client give when Requesting my API (if they only provide name , then my Model only update the name field without updating the email field). Is there any best practice for doing that?

You can get the user you are working with, assign the received parameters and save the user

For example:

const user = await User.findByPk(id);
Object.assign(user, params);
await user.save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM