Fjerda
Fjerda

Reputation: 35

Mongoose findOneAndUpdate won't update my database

I am teaching myself NodeJS while working on an app. I use Mongoose and Axios.

On one section of the app I display the partners and when I click on update I get a form with the informations of the selected partner in order to update them.

The client-side sends in the infos to the server but the server won't update the entries in the database. Here is my server side

app.post("/api/partner/update", (req, res) => {
const id = req.body.id;
const fullname = req.body.fullname;
const email = req.body.email;
const phones = req.body.phones;
const shops = req.body.shops;
const company = req.body.company;

console.log("The body is ",req.body) //It returns the correct data from the client's side submited data

Partner.findOneAndUpdate(id, mongoose.set('useFindAndModify', false),
 {
    "fullname": fullname,
    "email": email,
    "phones": phones,
    "shops":shops,
    "company": company
  },
  (err, document) => {
    if (err) return err;
    //console.log(document);
    res.send({ document });  

  }
);});

Here is my model :

 const mongoose = require("mongoose");

const partnerSchema = mongoose.Schema(
  {
    fullname: {
      type: String,
      maxlength: 50,
      required: true
    },
    email: {
      type: String,
      trim: true,
      unique: 1,
      required: true
    },
    phones: {
      type: Array
    },
    company: {
      type: String,
      maxlength: 50,
      required: true
    },
    shops: {
      type: Array,
      default: 0
    },
    is_valid: {
      type: Boolean
    },
    validated_by: {
      type: String
    },

    created_by: {
      type: String,
      maxlength: 50
    },
    updated_by: {
      type: String
    },
    deleted_by: {
      type: String
    },
    deleted_at: {
      type: Date,
      default: null
    }
  },
  {
    timestamps: {
      createdAt: "created_at",
      updatedAt: "updated_at"
    }
  }
);

const Partner = mongoose.model("Partner", partnerSchema)
module.exports ={Partner}

I don't understand why it is not updating the fields in the dataBase

Upvotes: 2

Views: 1207

Answers (2)

Sandeep Patel
Sandeep Patel

Reputation: 5148

This is a syntax for findOneAndUpdate as per the docs:

var query = { name: 'borne' };
Model.findOneAndUpdate(query, { name: 'jason bourne' }, options, callback)

Change db query to this:

let query = {_id: id };
let dataToUpdate= {
    "fullname": fullname,
    "email": email,
    "phones": phones,
    "shops":shops,
    "company": company
  }
let options = {useFindAndModify: false}  // useFindAndModify set to false at query level
// options = {useFindAndModify: false,new:true} if you want updated docs in return


 Partner.findOneAndUpdate(query,dataToUpdate,options,(err, document) => {
        if (err) return err;
        //console.log(document);
        res.send({ document });  

      }
    )

To set Deprecation Warnings on or off at mongoose level use option

mongoose.set('useFindAndModify', false)

Read more here:

Deprecation Warnings

findOneAndReplace

You can also use findByIdAndUpdate method:

Model.findByIdAndUpdate(id, update, options, callback)

You can enable promises to make code more readable and testable: https://mongoosejs.com/docs/promises.html

Upvotes: 1

Subin Sebastian
Subin Sebastian

Reputation: 10997

Refering the doc, https://mongoosejs.com/docs/tutorials/findoneandupdate.html first parameter of findOneAndUpdate should be a filter object, not the id directly.

So please try Partner.findOneAndUpdate({'_id': id},....

Upvotes: 1

Related Questions