dec0mpiled
dec0mpiled

Reputation: 99

Cannot update mongoose attribute with .update()

I have created a register system for my site that sends a confirmation email to the user who signed up, with their confirmation code being the _id of their account in the mongoose database.

Then, back on my site, they enter their confirmation code, and I want to use an .update() method to change the value of the confirmed attribute from 0 to 1 in the database for that user.

Here is my code:

router.post('/confirmAcc',function(req, res, next) {

  var getid=req.body.confirmcode;
  console.log(getid);
  var requis = { "_id": getid };

  User.update(requis, {confirmed: '1'}, function (err, doc){
    console.log(doc);
    if (err) throw err;
  });

  res.render('index', {title:'nDoto'});
});

The database has no problem matching the id, but it doesn't update the existing "confirmed" attribute to "1".

I am not sure why.

Also, when I run the code, and put in my confirmation code and that function runs, the console writes:

{ ok: 0, n: 0, nModified: 0 }

Upvotes: 1

Views: 1286

Answers (2)

jack blank
jack blank

Reputation: 5195

Not sure if this will help but I wanted to say that the update method has a $set operator $set documentation.. The $set operator replaces the value of a field with the specified value. So if you still wanted to use update() maybe something like this would of worked:

db.catalog.update({"_id" : getid},{$set : {"confirmed" : "1"}})

This should change the "confirmed" field's value.

Upvotes: 1

dec0mpiled
dec0mpiled

Reputation: 99

I fixed the problem. Instead of using User.update(), I used:

router.post('/confirmAcc',function(req, res, next) {
var getid=req.body.confirmcode;

var requis = { "_id": getid };

User.findOne(requis, function (err, doc){
    doc.credentials.confirmed="1";
    doc.save();
    if (err) throw err;
});
res.render('index', {title:'nDoto'});
});

And this worked :)

Upvotes: 1

Related Questions