Let's say I have a User
model like this
var userSchema = new Schema({
username : String,
email : String,
project : {type : String, ref : "Project"}
});
and a User
document like this.
{
"_id" : ObjectId("56df56c58a4d47c83bf41603"),
"username" : "user1",
"email" : "email@example.com",
"project" : "",
"__v" : 1
}
If I do the following, the page never loads.
User.findById("56df56c58a4d47c83bf41603").populate("project").exec()
.then(function(userObj) {
res.render('user', {
user : userObj
});
});
It works fine if there is an actual ObjectID in there, but not when it is blank.
Is there a way that I can default to null if there is no ObjectID in the value?