简体   繁体   中英

Mongoose Error: `{“message”:“No matching document found.”,“name”:“VersionError”}`

I've been trying to figure out why my HTTP Put has not been working after I use it once. When I click a button, I push the current user's id into an array like so:

$scope.currentUser = {
  'eventsAttending' = [];
}

$scope.attending = function(event, id){

  if($cookieStore.get('token')){
    $scope.currentUser = Auth.getCurrentUser();
  }
  $scope.currentUser.eventsAttending.push(event._id);
  $http.put('/api/users/' + $scope.currentUser._id, $scope.currentUser)
    .success(function(data){
      console.log("Success. User " + $scope.currentUser.name);
    });
}

And my HTTP Put function is like so:

var express = require('express');
var controller = require('./user.controller');
var config = require('../../config/environment');
var auth = require('../../auth/auth.service');
router.get('/:id', controller.getEvents);

var router = express.Router();

exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  User.findById(req.params.id, function (err, user) {
    if (err) { return res.send(500, err); }
    if(!user) { return res.send(404); }
    var updated = _.merge(user, req.body);
    updated.markModified('eventsAttending');
    updated.save(function (err) {
      if (err) { return res.send(500, err); }
      return res.json(200, user);
    });
  });
};

In my HTML page I have multiple events I can attend, and each event has the button called Attend where I call $scope.attending and the function is called and the HTTP Put occurs. All of this works for the first event I choose to attend. However, when I click the Attend button for another event, I get an error that says:

{"message":"No matching document found.","name":"VersionError"}

And I have no idea why. The error occurs when I try to do updated.save() in the mongoose call and I get res.send(500, err)

I tried to look at http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html to solve the issue as I did some googling but I keep getting an error that says:

Undefined type at `versionKey` 
Did you try nesting Schemas? You can only nest using refs or arrays.

Upon adding into my schema:

var UserSchema = new Schema({ versionKey: 'myVersionKey', ...

I also tried to change the .save() function into .update() as someone suggested it online but that seemed to give me even more errors. Any ideas how to fix this? That would be much appreciated!

Take a look at your syntax on the lines:

$scope.currentUser = {
    'eventsAttending' = [];
}

I'm fairly certain assignment inside of a JSON structure like this can cause errors, especially when you try to bring that structure into MongoDB.

I think the issue you may be experiencing (as was the case when I was getting this error from a similar action) is that after the first update, the '__v' VersionKey property on the newly updated document has changed, but you might not be updating that property on the object you have in the browser. So when you go to update it again, you're sending the old '__v' VersionKey, (even though you updated the 'eventsAttending' property) and that document conflicts the newer VersionKey. This would assume that the Auth.getCurrentUser(); function returns the whole document object from mongo.

What I did to fix this was simply add delete entity.__v; to delete the old VersionKey from the document before sending it with the request. Better yet, I'd recommend updating the properties your API sends back when returning documents so this issue doesn't happen in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM