findOneAndUpdate 有时更新有时不更新

问题描述

好的,所以我有一个如下的架构

const newUserSchema = new mongoose.Schema({
  name: {
    type: String,required: [true,"Check Data Entry,no name specified"]
  },email: {
    type: String,no email specified"]
  },password: {
    type: String,// required: [true,no password specified"]
  },kids: []
});

然后我创建了新的孩子模式

const newKidSchema = new mongoose.Schema({
  name: {
    type: String,age: {
    type: Number,no age specified"]
  },gender: {
    type: String,no level specified"]
  },experiencePoints: {
    type: Number
  },gamescores: [],learningResources: [],progress: [],engPlanner: [],urduPlanner: [],mathPlanner: [],dates: [],dayTaskLength:[]
});
const learningResources = new mongoose.Schema({
  name: {
    type: String,subject: {
    type: String,no subject specified"]
  },status: {
    type: Boolean,no status specified"]
  },learningTime: {
    type: String,no time specified"]
  }
});

const gamescoreSchema = new mongoose.Schema({
  subject: {
    type: String,gameTitle: {
    type: String,no Game Title specified"]
  },gamescore: {
    type: Number,no Game score specified"]
  },gameTime: {
    type: String,no Game Time specified"]
  },experiencePoints: {
    type: Number,no Experience Points specified"]
  },gameStatus: {
    type: Boolean,no Game Status specified"]
  }
});
const progressSchema = new mongoose.Schema({
  engGamesProgress:{
    type: Number,required: [true]
  },mathGamesProgress:{
    type: Number,urduGamesProgress:{
    type: Number,englrProgress:{
    type: Number,mathLrProgress:{
    type: Number,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.logkid.gamescores 数组中找到我新添加的分数时,但是当我 console.log(kids) 时它没有更新。在 mongodb Atlas 中,我找不到新的游戏分数。但问题是它们偶尔会被输入到数据库中。我很困惑为什么会出现这个问题。我知道我正在发送正确的数据,并且它正在kid.gamescores 数组中更新,但“Kid”没有使用新的gamescore 数组更新。

User.findOneAndUpdate({
        email: signedInUser
      },{
        kids:gameKidsArray
      }

这是我更新阵列的地方。 任何帮助。

解决方法

如果要确保 findOneAndUpdate 成功更新您的记录,则需要在最后一个参数中添加 { new: true }。见here

所以你应该更新你的代码

User.findOneAndUpdate({
        email: signedInUser
      },{
        kids:gameKidsArray
      },{
        new: true
      }