mongodb TypeError:Customer.update()不是函数

u5rb5r59  于 2023-06-29  发布在  Go
关注(0)|答案(3)|浏览(86)

当我执行其他函数,如查找,添加数据库,然后他们运行得很好,但当我更新数据库条目,然后我得到这个错误,我使用mongodbMap集,有人能帮助我如何解决这个错误。这是代码的github链接:Github URL
这就是我得到的错误

我在互联网上搜索,但没有找到任何解决方案,解决我的问题,所以这就是为什么我问一个新的问题,类似的问题已经被问到,但没有在我的情况下工作

nimxete2

nimxete21#

使用Model.findOneAndUpdate()
https://mongoosejs.com/docs/tutorials/findoneandupdate.html

// update customer
const updateCustomer = (_id, customer) => {
  Customer.findOneAndUpdate({ _id }, customer).then((customer) => {
    console.info("Customer Updated");
    db.close();
  });
};
k75qkfdt

k75qkfdt2#

从我在库中看到的 * Customer.update()* 没有作为独立函数公开,而是有一个标识符 updateCustomer 可以调用...

const updateCustomer = (_id, customer) => {
  Customer.update(_id , customer).then((customer) => {
    console.info("Customer Updated");
    db.close();
  });
};

所以像这样直接用。。

updateCustomer(_id, customer);
62lalag4

62lalag43#

您的更新查询应该如下所示:
您可能希望同时查找by _id和update:

const customersData = {name: "test name", age: 24}

Customer.findByIdAndUpdate(_id, customersData)

相关问题