In my application, users will be able to change their names.
I want existing games to reflect this.
Current setup...
var PlayerSchema = new Schema({
name: 'Foo',
// ...
});
This example is working...
var PlayerSchema = new Schema({
userid: ObjectId,
// ...
});
PlayerSchema.virtual('name').get(function () {
return 'Bar';
});
var GameSchema = new Schema({
players: [PlayerSchema],
// ...
}, { toJSON: { virtuals: true } });
module.exports.getGame = function (id, cb) {
Game.findOne({ _id: id }, 'players', function (err, doc) {
cb(doc);
});
};
socket.on('getGame', function (id, fn) {
db.getGame(id, function (game) {
fn(game);
// [{ _id: 5016bab0562cb81c6abe1811, name: 'Bar' }]
});
});
Async getters does not...
PlayerSchema.virtual('name').get(function () {
User.findOne({ _id: this.userid }, function (err, doc) {
return
doc.name;
});
});
Is this the proper solution to this kind of scenario?
http://stackoverflow.com/questions/11981456/mongoose-getter-function-doesnt-return-updated-array-when-making-async-calls