1

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.

Stefan Medack
  • 2,731
  • 26
  • 32

1 Answers1

1

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.

Stefan Medack
  • 2,731
  • 26
  • 32
  • I have a schema that has a subdoc ref that is originally filled with a valid value. I'm able to set the ref value to undefined without issue. But setting a valid value back to the now undefined ref field produces a Cast to ObjectId fail error. Is all of this related? I'm using Mongoose ~3.8.8. – Prasad Silva Feb 24 '15 at 07:53
  • You're problem seems to be a different one, since you can retrieve documents with filled values. My problem is actually that the _id is not filled with a valid value which makes modifying the retrieved document impossible. – Stefan Medack Feb 24 '15 at 08:28
  • Yea, it is different. It's related to the fact that I'm attempting to update a populated document via a MEAN.js update scaffold for Express. I'm looking at different ways to work around this, but they're all quite ugly (e.g.: re-query the parent model, and manually fill in the details from the request in the Express controller). I'd have hoped that Mongoose would at least detect this case and provide better feedback. – Prasad Silva Feb 24 '15 at 15:59