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 _id
is 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 findOneAndUpdate
for example does freeze the whole node.js app, without any log. It just doesn't continue.