0
votes

Using mongoose populate: http://mongoosejs.com/docs/populate.html

It seams that mongoose is forcing me to declare a ref value for populate when I first create the document but in my case i don't have the ref info yet. When I try to create a new document while providing an empty string I get to my developer field I get:

{"message":"Cast to ObjectId failed for value \"\" at path \"developer\"","name":"CastError","type":"ObjectId","value":"","path":"developer"}

Object that I'm saving through mongoose:

var Project = {
  name: 'Coolproject',
  status: 'pending',
  developer: '',
  type: 'basic',
};

Project.create(req.body, function(err, project) {
  if(err) { return handleError(res, err); }
  return
});

My Model:

var ProjectSchema = new Schema({
  name: String,
  status: {type:String, default:'pending'},
  developer:{type: Schema.Types.ObjectId, ref: 'User'},
  type:String
});

Basically I need to set it later, but it doesn't seam like this is possible. Currently my work around is populate it with a dummy user until later but this is less than desirable.

Thoughts?

Update

Realized that if i provide a object id like value (55132a418b3cde5546b01b37) it lets me save the document. Very odd. Guess it just figured it can find the document moves on. Wondering why this doesn't happen for a blank value.

2
I don't understand what you mean. Is it not letting you create a document without defining a user for the developer? or is it not letting you define the schema without the User model existing. How does populate tie into this? question looks very X/Y ish. - Kevin B
Ahhh, yeah very true. Updated it to hopefully be a little more clear. I want to be able to save a reference later, and not when I initially created the parent document. - Justin
Is this error happening when you are creating a new document? you said save, but if you were saving that would indicate that you were at least able to at some point create the document. Can you include the code that is causing this error? - Kevin B
I added my model for some clarity. Yes this is on new document creation, and its not letting me create it without providing a value. It works great when I provide a id to a document, but not when I just leave it blank. - Justin
Can you add the part where you try to create/save the document and get the error? Are you saying something like var p = new Project(); p.save(); fails? - numbers1311407

2 Answers

1
votes

The problem is explained in the error message. You cannot save an Empty String in the place of an ObjectId. The field is not listed as 'required', so there is no problem leaving it out and saving the document.

Code correction:

// you can save this
var Project = {
    name: 'Coolproject',
    status: 'pending',
    type: 'basic',
};
-1
votes

You need to use the sparse index in model. So, the valid model can have developer equal to nil

var ProjectSchema = new Schema({
  name: String,
  status: {type:String, default:'pending'},
  developer:{type: Schema.Types.ObjectId, ref: 'User', sparse:true},
  type:String
});

See http://mongoosejs.com/docs/api.html#schematype_SchemaType-sparse and http://docs.mongodb.org/manual/core/index-sparse/ for additional info