我有以下代码:当我尝试从邮递员那里说:
“ message”:“ account.save不是函数”
const account = await Account.find({ "buildings.gateways.devices.verificationCode": code })
var accountId = account ? account.map(item => item._id) : null
const buildings = _.flatMap(account, a => a.buildings)
const gateways = _.flatMap(buildings, b => b.gateways);
const devices = _.flatMap(gateways, g => g.devices);
// finding deviceId to insert for user from that account
const device = _.filter(devices, d => d.verificationCode === code);
device.patientFirstName = req.body.firstName;
device.patientLastName = req.body.lastName;
account.save();
如果我尝试更改为findOne(),它会说
“ account.map不是函数”
帐户总是返回值,然后为什么它不能映射我不明白。
感谢您的帮助。谢谢
.find()
返回一个数组,这样您就可以运行,Array.map()
但是需要分别处理.save()
每个文档:
const accounts = await Account.find({ "buildings.gateways.devices.verificationCode": code })
var accountId = account ? account.map(item => item._id) : null
for(let account of accounts){
await account.save();
}
.findOne()
(已等待)返回单个文档,因此您不能使用.map()
:
const account = await Account.findOne({ "buildings.gateways.devices.verificationCode": code })
var accountId = account ? account._id : null
account.save();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句