我创建了以下架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Player = require('./player');
var gameSchema = new Schema({
created_at: Date,
nrOfCards: String,
players: [{
sticks: String,
player: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
}
}],
});
var Game = mongoose.model('Game', gameSchema);
保存部分工作正常,保存的对象可能如下所示:
"_id": "57dd11aca0c36114588fd250",
"nrOfCards": "3",
"__v": 0,
"players": [
{
"_id": "57d415e527c20f3ed2416e05",
"age": "33"
},
{
"_id": "57d417df2186d53f3d49c996",
"age": "73"
},
{
"_id": "57d41d85ec315d4234010c7d",
"age": "20"
}
]
},
在保存了一个对象之后,我希望在填充了player-field的情况下返回它。这是我的尝试:
app.post('/api/games', function(req, res) {
Game.create({
players : req.body.activePlayers,
nrOfCards: req.body.nrOfCards,
}, function(err, game) {
if (err) {
res.send(err);
} else {
Game.findOne(game)
.populate('players.player')
.exec(function (err, newgame) {
if (err) return handleError(err);
console.log(newgame);
res.json(newgame);
});
}
});
});
认为.populate('players.player')
应该可以解决这个问题,但我只是接收了包含_id
player
#div1{
height:50px;
overflow:hidden;
}
#div1:hover{
height:300px;
-moz-transition: .4s all ease-in-out;
-ms-transition: .4s all ease-in-out;
-webkit-transition: .4s all ease-in-out;
-o-transition: .4s all ease-in-out;
}
的未填充字段。
小贴士表示赞赏。谢谢!
答案 0 :(得分:0)
使用
player: {
type: Schema.Types.ObjectId,
ref: 'Player'
}
进入您的架构。