skip to Main Content

When I am executing other funcitons like find, add in database then they are running perfectly fine , but when I am updating the database entry then I am getting this error , I am using mongodb atlas, can someone help me how to solve this error. This is the github link of the code : Github URL

This is the error which I am getting

I searched on internet but didn’t find any solution which fixes my problem, so that’s why I am asking a new question, a similar kind of question already being asked but that didn’t work in my case

3

Answers


  1. Your update query should look like this:

    You may want to find by _id and update at the same time:

    const customersData = {name: "test name", age: 24}
    
    Customer.findByIdAndUpdate(_id, customersData)
    
    Login or Signup to reply.
  2. Use 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();
      });
    };
    
    Login or Signup to reply.
  3. From what I’m seeing in that library Customer.update() is not exposed as a standalone function but instead there’s the identifier updateCustomer one can call…

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

    So use it directly like this…

    updateCustomer(_id, customer);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search