【发布时间】:2021-06-01 20:57:13
【问题描述】:
好的,所以我有一个如下架构
const newUserSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Check Data Entry, no name specified"]
},
email: {
type: String,
required: [true, "Check Data Entry, no email specified"]
},
password: {
type: String,
// required: [true, "Check Data Entry, no password specified"]
},
kids: []
});
然后我创建了新的孩子架构
const newKidSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Check Data Entry, no name specified"]
},
age: {
type: Number,
required: [true, "Check Data Entry, no age specified"]
},
gender: {
type: String,
required: [true, "Check Data Entry, no level specified"]
},
experiencePoints: {
type: Number
},
gameScores: [],
learningResources: [],
progress: [],
engPlanner: [],
urduPlanner: [],
mathPlanner: [],
dates: [],
dayTaskLength:[]
});
const learningResources = new mongoose.Schema({
name: {
type: String,
required: [true, "Check Data Entry, no name specified"]
},
subject: {
type: String,
required: [true, "Check Data Entry, no subject specified"]
},
status: {
type: Boolean,
required: [true, "Check Data Entry, no status specified"]
},
learningTime: {
type: String,
required: [true, "Check Data Entry, no time specified"]
}
});
const gameScoreSchema = new mongoose.Schema({
subject: {
type: String,
required: [true, "Check Data Entry, no subject specified"]
},
gameTitle: {
type: String,
required: [true, "Check Data Entry, no Game Title specified"]
},
gameScore: {
type: Number,
required: [true, "Check Data Entry, no Game Score specified"]
},
gameTime: {
type: String,
required: [true, "Check Data Entry, no Game Time specified"]
},
experiencePoints: {
type: Number,
required: [true, "Check Data Entry, no Experience Points specified"]
},
gameStatus: {
type: Boolean,
required: [true, "Check Data Entry, no Game Status specified"]
}
});
const progressSchema = new mongoose.Schema({
engGamesProgress:{
type: Number,
required: [true]
},
mathGamesProgress:{
type: Number,
required: [true]
},
urduGamesProgress:{
type: Number,
required: [true]
},
engLrProgress:{
type: Number,
required: [true]
},
mathLrProgress:{
type: Number,
required: [true]
},
urduLrProgress:{
type: Number,
required: [true]
}
});
这是我接收 gameScore 数据的代码,
app.post("/add-game-score", (req, res) => {
// New game
const newSubject = req.body.subject;
const newTitle = req.body.gameTitle;
const newGameScore = req.body.gameScore;
const newGameTime = req.body.gameTime;
const newExperiencePoints = req.body.experiencePoints;
const newgameStatus = req.body.gameStatus;
User.findOne({
email: signedInUser
}, function(err, foundList){
if(!err){
if(foundList){
kids = foundList.kids;
const newgame = new GameScore({
subject: newSubject,
gameTitle: newTitle,
gameScore: newGameScore,
gameTime: newGameTime,
experiencePoints: newExperiencePoints,
gameStatus: newgameStatus
});
// var new_kids = [];
kids.forEach(kid => {
if(kid._id == kidProfileCurrentlyIn.kidID){
kid.gameScores.push(newgame)
console.log("Bellow are all game scores");
console.log(kid.gameScores);
}
// new_kids.push(kid);
});
gameKidsArray = kids;
User.findOneAndUpdate({
email: signedInUser
}, {
kids:gameKidsArray
}, (err, foundList) => {
if(foundList){
console.log(gameKidsArray);
console.log("Added Game Successfully");
console.log(foundList);
}
});
}
}
});
});
现在的问题是,当我在 console.log child.gameScores 数组中找到我新添加的分数时,但是当我 console.log(kids) 时它没有得到更新。在 mongodb Atlas 中我找不到新的游戏分数。但问题是它们有时会被输入数据库。我很困惑为什么会出现这个问题。我知道我正在发送正确的数据,并且它正在 child.gameScores 数组中更新,但“Kid”没有更新为新的 gamescore 数组。
User.findOneAndUpdate({
email: signedInUser
}, {
kids:gameKidsArray
}
这是我更新我的数组的地方。 任何帮助。
【问题讨论】:
-
在当前代码中,您执行
gameKidsArray = kids,这基本上是您原来的 kids 数组。还更好地创建新数组,而不是弄乱来自猫鼬模型的数组。 -
问题是 gamesKidsArray 确实有新添加的值,但孩子们不会更新。什么问题,因为我知道它有一些数组问题,我会解决它。但是为什么它不使用新的游戏分数更新用户模式内的数组。
标签: node.js mongodb mongoose mongodb-query findoneandupdate