Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js doc.save()不是一个函数_Node.js_Reactjs_Mongoose - Fatal编程技术网

Node.js doc.save()不是一个函数

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

我试图使用.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 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
,这样就行了。老兄,经过漫长的一天,即使是最简单的事情也很难做到。谢谢