Discuss / JavaScript / 想要更新数据,对查询到的实例调用save()方法

想要更新数据,对查询到的实例调用save()方法

Topic source

kb466

#1 Created at ... [Delete] [Delete and Lock User]

(async () => { var p = await Pet.findAll({ where: { name: 'Gaffey' } }); p.gender = true; p.updatedAt = Date.now(); p.version ++; await p.save(); })();

运行后显示如下错误: Executing (default): SELECT id, name, gender, birth, createdAt, updatedAt, version FROM pets AS pet WHERE pet.na me = 'Gaffey'; (node:3364) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: p.save is not a function (node:3364) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

请问为什么,怎样解决这个问题?

kb466

#2 Created at ... [Delete] [Delete and Lock User]

此问题已经解决,谢谢大家!

我也是和你一样的代码,一样的错误提示。后来想起了findAll返回的是一个数组,然后代码改成如下就可以了。 for(let o of p){ o.gender = true; o.updatedAt = Date.now(); o.version ++; await o.save(); }

你使用findById()或者findOne()就可以了


  • 1

Reply