Cast to ObjectId failed for value \"[object Object]\" at path \"players\"

I'm just starting out with Mongoose and MongoDB. I've tried everything i could find here on stackoverflow, however, i'm lost as i do not know what the problem is.

I have the following 2 models, which reside in 2 different files:

var UserSchema = require('mongoose').model('User').schema;
var tournamentSchema = mongoose.Schema({
    tournamentName: String,
    nrOfPlayers: String,
    players: [{ type: mongoose.Schema.Types.ObjectId, ref: UserSchema }],
    openForLeagues: {
        leagues: []
    },
    edition: String,
    description: String,
    startDate: Date,
    endDate: Date,
    startHour: String,
    prize: Boolean,
    sponsors: String,
    ingameChatChannel: String,
    twitchStreamChannel: String
});

module.exports = mongoose.model('Tournament', tournamentSchema);

var userSchema = mongoose.Schema({

    local: {
        nickname: String,
        battlenetid: String,
        email: String,
        password: String,
        race: String,
        league: String,
        role: String,
        website: String
    }
});

module.exports = mongoose.model('User', userSchema);

Inside my routes.js file, i do the following:

app.post('/signup-tournament/:_id/:userId', isLoggedIn, function(req, res) {
    var playerId = req.params.userId;
    var nickname = req.user.local.nickname;

    console.log(ObjectId.isValid(req.params.userId)); //true
    console.log(ObjectId.isValid(req.params._id)); //true
    if (ObjectId.isValid(playerId) && ObjectId.isValid(req.params._id)) {
        Tournament.findByIdAndUpdate(req.params._id, {
            $pushAll: {
                players: {
                    values: [playerId, nickname]
                }
            }
        }, function(err, tournament) {
            if (err)
                res.send(err)
            else
                res.redirect('/signup-tournament/' + req.params._id + req.params.userId);
        });
    }
});

When i do the post, i get the following error:

{
    "message": "Cast to ObjectId failed for value \"[object Object]\" at path \"players\"",
    "name": "CastError",
    "type": "ObjectId",
    "value": [{
        "values": ["53e340b3afb9657f0dbcf994", "cristidrincu"]
    }],
    "path": "players"
}

I do not understand why there is a cast error when the ids are validated using ObjectId.isValid()... I've spent 2 days trying to figure this on my own, so please help :) Thank you!

You;re trying to perform findByIdAndUpdate operation which is against your tournamentSchema. The problem is in the following code:

Tournament.findByIdAndUpdate(req.params._id, {
  $pushAll: {
    players: {
      values: [playerId, nickname]
    }
  }
})

players should contain User objects, so it should be either player's _id (hex String or ObjectId), or valid User object with _id field.

But you're trying to push invalid object { values: [playerId, nickname] } instead.

Here is how this operation should look:

Tournament.findByIdAndUpdate(req.params._id, {
  $pushAll: {
    players: [playerId]
  }
})

N.B. Not sure what does nickname mean in your example, but it doesn't look like a valid ObjectId, so I removed it.