This basic Mongoose model code returns a null result (gameMgr) on the initial call, but works on subsequent calls. Shouldn't it return a populated result object on the initial upsert? The mongo record exists, and running the code a second time in the identical situation, everything works fine as there is no insert.
If a null result object is expected, what is the appropriate pattern to create a new object? (or do I reload with another db call and descend further into callback madness?)
GameManagerModel.findByIdAndUpdate(
game._id,
{$setOnInsert: {user_requests:[]}},
{upsert: true},
function(err, gameMgr) {
console.log( gameMgr ); // NULL on first pass, crashes node
gameMgr.addUserRequest( newRequest );
}
);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…