Alwaysblue
Alwaysblue

Reputation: 11930

Unable to Update Mongoose Value

I have a mongoose Schema which looks like this

var mongoose = require("mongoose");
var UserSchema = new mongoose.Schema ({
  username: String,
  image: String,
  userId: String,
  email: String,
  isFormFilled: {
      default: false,
      type: Boolean
  }, 
});

module.exports = mongoose.model("user", UserSchema);

Now, The first time I put value in it, I don't change/put isFormFilled value and hence it is value.

Now, somewhere in the app, where I want to change the value of isFormFilled to true I am importing it to the given folder containing the route

const userBasic = require("../models/user-model.js")

and in the route I am doing something like this

 userBasic.findOneAndUpdate({userId: newUser.userid},  {
            isFormFilled: true
        },  (err, user) => {
            if (err) {
                console.log(error)
            } else {
            console.log(user)
            res.json(req.user)
            }
        })

but it doesn't seem to be updating my value to true.

Question: Any Idea what I could be doing wrong and how I can fix it?

Upvotes: 0

Views: 33

Answers (1)

Check it with Robo 3T, and if it has changed, I have a guess. Mongoose findOneAndUpdate has a parameter called options, and you have to pass a {new: true} as an argument (the third argument, before the callback), in order to get the changed document.

Edit: yep, as mentioned in the comments seconds before me :)

Upvotes: 1

Related Questions