Node.js doc.save()不是一个函数
我试图使用.save()方法更新Mongo文档,但它会不断输出Node.js doc.save()不是一个函数,node.js,reactjs,mongoose,Node.js,Reactjs,Mongoose,我试图使用.save()方法更新Mongo文档,但它会不断输出doc。save()不是函数 这是我的密码: // cart.js const Cart = require('../models/Cart'); //Contains the requirements for Mongoose // My React app handles all the changes to the cart and then passes // in the updated cart along with
doc。save()不是函数
这是我的密码:
// cart.js
const Cart = require('../models/Cart'); //Contains the requirements for Mongoose
// My React app handles all the changes to the cart and then passes
// in the updated cart along with the customerID from the body
const doc = await Cart.find({ customerID }); // customerID = '12345'
doc.cart = cart;
doc.save();
购物车的内容:
]
{
name: 'Item Name',
sku: '2233',
price: 14.99,
qty: 1
}
{
name: 'Item Name 2',
sku: '4455',
price: 13.99,
qty: 2
}
]
运行console.log(doc)
时来自Mongo的当前响应:
{
_id: ##################,
cart: [
{
name: 'Item Name',
sku: '2233',
price: 14.99,
qty: 1
}
],
customerID: '12345',
date: 2020-03-04T20:49:05.761Z,
__v: 0
}
如果在保存过程后运行console.log(doc)
,则预期输出:
{
_id: ##################,
cart: [
{
name: 'Item Name',
sku: '2233',
price: 14.99,
qty: 1
}
{
name: 'Item Name 2',
sku: '4455',
price: 13.99,
qty: 2
}
],
customerID: '12345',
date: 2020-03-04T20:49:05.761Z,
__v: 0
}
调用函数时的控制台输出:doc.save()不是函数
也许是因为我已经编写了8个小时的代码,但我没有看到我遗漏了什么。帮助。等待的查找
返回一个数组。要获取单个文档,请使用findOne
:
const doc = await Cart.findOne({ customerID });
等待的查找
返回一个数组。试试findOne
,这样就行了。老兄,经过漫长的一天,即使是最简单的事情也很难做到。谢谢