1
votes

I'm currently on a very weird mongoose error and I don't know what is causing this problem. When I call findOne method, I get a valid instance, with only the _id field not initialized. When I'm trying to save the changes in that document it crashes with

{ [CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"]
 message: 'Cast to ObjectId failed for value "[object Object]" at path "_id"',
 name: 'CastError',
 type: 'ObjectId',
 value: 
   { from_node_id: 52e10f6acce9daa7f3bf3162,
     target_id: 'a' },
 path: '_id' }

I have a simple Schema Definition:

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var transitionProbabilitySchema;
transitionProbabilitySchema = new Schema({
   _id: {
       from_node_id: {type: Schema.Types.ObjectId, ref: "Zone"}
   },
   value: {
       total_transition_count: Number,
       probabilities: [
           {to_node_id: {type: Schema.Types.ObjectId, ref: "Zone"}},
           {probability: Number}
       ]}
}, {collection: 'transitionProbability'});

module.exports = mongoose.model('transitionProbability', transitionProbabilitySchema);

and when I now call

TransitionProbability.findOne({"_id.from_node_id": from_node_id},
        function (err, tp) {
            if (err) {
                handleError('error - tp not found', err, callback);
            } else {
                 // some modification of tp and then save
            }
        }

Mongoose returns the document, but only _idis not initialized. The whole function crashes when I am trying to save the altered document and not before. I also logged the document to the console to verify it is just the `_id' field missing.

PS: note, that the from_node_id is extracted from another query and is a valid ObjectId.

Any ideas? Thanks in advance for your help!

Edit: setting the _id field manually does not seem to work either. Even more interesting: while trying out other methods, I recognized that findOneAndUpdatefor example does freeze the whole node.js app, without any log. It just doesn't continue.

1

1 Answers

0
votes

Edit: For your information: after searching for two days straight without finding an answer I created a ticket for a possible bug at Mongoose.js Github and they confirmed my problem. According to them it is fixed in the new 4.0.0 release candidate, which is not recommended for productive use. At actually solved my problem, but the rc1 made even more problems.


My solution so far:

Finally I was so annoyed by this error, that I changed my whole cumulation of that table so that _id does not have a separate from_node_id field. I use the from_node_id now as the ID directly.