gpt4 book ai didi

javascript - 添加到父对象的 Mongoose 子对象未正确保存

转载 作者:行者123 更新时间:2023-12-03 02:40:45 26 4
gpt4 key购买 nike

我有一个名为“玩家”的架构,另一个名为“游戏”的架构。每个游戏都有一个名为“玩家”的属性,它是对玩家对象的引用数组。

游戏架构

let GameSchema = new mongoose.Schema({
players: [{
ref: "Player",
type: mongoose.Schema.Types.ObjectId,
}],
created: {
type: Date,
default: Date.now,
}
}, { usePushEach: true });

玩家架构

let PlayerSchema = new mongoose.Schema({
parentGameId: mongoose.Schema.Types.ObjectId,
idInGame: Number,
points: Number,
});

MongoDb 连接

mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/ScoreKeeper", { useMongoClient: true });

用于在后期路线上初始化游戏和玩家的代码。 (我使用的是 Express)

app.post('/', function(req, res) {
let players = Number(req.body.players);
Game.create({
players: [],
}, function(error, newGame) {
if (error) {
console.log(error);
} else {
let currentGameID = newGame._id;
for (let i = 1; i <= players; i++) {
Player.create({
parentGameId: currentGameID,
idInGame: i,
points: 0
}, function(error, newPlayer) {
if (error) {
console.log(error);
} else {
newGame.players.push(newPlayer._id);
newGame.save(function(error, newGame) {
if (error) {
console.log(error);
} else {
console.log("New Player added");
}
});

}
});
}
}

});
});

在节点控制台的终端上,当我创建 5 个玩家时,我有以下输出:

New Player added

New Player added

New Player added

New Player added

New Player added

现在,当我进入 mongo 控制台并查看玩家集合时,我发现按照预期创建了 5 个玩家。使用“db.players.find()”

{ "_id" : ObjectId("5a6159685d3dabb00d0065b0"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 1, "points" : 0, "__v" : 0 }

{ "_id" : ObjectId("5a6159685d3dabb00d0065b1"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 2, "points" : 0, "__v" : 0 }

{ "_id" : ObjectId("5a6159685d3dabb00d0065b2"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 3, "points" : 0, "__v" : 0 }

{ "_id" : ObjectId("5a6159685d3dabb00d0065b3"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 4, "points" : 0, "__v" : 0 }

{ "_id" : ObjectId("5a6159685d3dabb00d0065b4"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 5, "points" : 0, "__v" : 0 }

但是,当我在 mongo 控制台中检查游戏对象时,我发现......

db.games.findOne()

{

"_id" : ObjectId("5a6159685d3dabb00d0065af"),

"created" : ISODate("2018-01-19T02:35:20.632Z"),

"players" : [

ObjectId("5a6159685d3dabb00d0065b1"),

ObjectId("5a6159685d3dabb00d0065b1"),

ObjectId("5a6159685d3dabb00d0065b0"),

ObjectId("5a6159685d3dabb00d0065b1"),

ObjectId("5a6159685d3dabb00d0065b0"),

ObjectId("5a6159685d3dabb00d0065b2"),

ObjectId("5a6159685d3dabb00d0065b3"),

ObjectId("5a6159685d3dabb00d0065b3"),

ObjectId("5a6159685d3dabb00d0065b4")

],

"__v" : 5

}

游戏和玩家之间不匹配,我觉得错误在于添加newPlayer后保存newGame。奇怪的是,_v 属性按预期读取 5。您能指出我修复该错误吗?

谢谢

最佳答案

您的代码中保存游戏文档时存在错误

使用 update 方法和 push 代替 save 来添加玩家的 ObjectId

newGame.update({$push:{players : newPlayer._id} }, function(error, newGame) {...}

控制台输出

Mongoose: games.insert({ _id: ObjectId("5a6168c12478110e7aa74d5d"), created: new Date("Fri, 19 Jan 2018 03:40:49 GMT"), players: [], __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 1, points: 0, _id: ObjectId("5a6168c12478110e7aa74d5e"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 2, points: 0, _id: ObjectId("5a6168c12478110e7aa74d5f"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 3, points: 0, _id: ObjectId("5a6168c12478110e7aa74d60"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 4, points: 0, _id: ObjectId("5a6168c12478110e7aa74d61"), __v: 0 })
Mongoose: players.insert({ parentGameId: ObjectId("5a6168c12478110e7aa74d5d"), idInGame: 5, points: 0, _id: ObjectId("5a6168c12478110e7aa74d62"), __v: 0 })
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d62") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d5f") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d61") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d60") } }, {})
Mongoose: games.update({ _id: ObjectId("5a6168c12478110e7aa74d5d") }, { '$push': { players: ObjectId("5a6168c12478110e7aa74d5e") } }, {})

mongo CLI

> db.players.find()
{ "_id" : ObjectId("5a6168c12478110e7aa74d5f"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 2, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d61"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 4, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d5e"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 1, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d60"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 3, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6168c12478110e7aa74d62"), "parentGameId" : ObjectId("5a6168c12478110e7aa74d5d"), "idInGame" : 5, "points" : 0, "__v" : 0 }
>
>
> db.games.find().pretty()
{
"_id" : ObjectId("5a6168c12478110e7aa74d5d"),
"created" : ISODate("2018-01-19T03:40:49.028Z"),
"players" : [
ObjectId("5a6168c12478110e7aa74d62"),
ObjectId("5a6168c12478110e7aa74d5f"),
ObjectId("5a6168c12478110e7aa74d61"),
ObjectId("5a6168c12478110e7aa74d60"),
ObjectId("5a6168c12478110e7aa74d5e")
],
"__v" : 0
}
>

关于javascript - 添加到父对象的 Mongoose 子对象未正确保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48333382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com