【发布时间】:2015-06-26 10:45:19
【问题描述】:
我在 MEAN.js 上运行,但在将新子文档推送到现有数据库条目时遇到问题。到目前为止我所做的: 我有以下数据库架构:
var GameSchema = new Schema({
title: {
type: String,
trim: true,
required: 'Title cannot be blank',
unique: 'Title must be unique'
},
invitedPlayers: [{
email: {
type: String
},
resources: {
wood: {
type: Number,
default: 0
},
stone: {
type: Number,
default: 0
},
gold: {
type: Number,
default: 0
}
}
}]
});
mongoose.model('Game', GameSchema);
我想将新条目推送到受邀玩家中。每当管理员填写带有用户邀请的表单并单击提交时,应用程序都会调用此方法:
// Invite new User
$scope.invite = function() {
// Create new Game object
var game = new AdminGamesService ({
_id: $stateParams.gameId,
invitedPlayers: $scope.player,
});
game.$update(function() {
$location.path('admin/users');
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
它在服务器上运行它(_ - 使用 lodash):
// Update game
exports.update = function(req, res) {
var game = req.game ;
game = _.extend(game , req.body); //here should be something that merge arrays instead of replacing them?
game.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(game);
}
});
};
但是整个代码覆盖了我较早被邀请的玩家并且只添加了新的:(我必须更改一些东西来推送数组受邀玩家中的条目,但我不知道是什么:)
【问题讨论】: