简体   繁体   English

findOneAndUpdate 有时更新有时不更新

[英]findOneAndUpdate updates sometimes and sometimes doesnt

Ok so i have a schema as follows好的,所以我有一个架构如下

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: []
});

then I created new kid schema然后我创建了新的孩子模式

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]
  }
});

And this is the code where i receieve the gameScore data,这是我收到 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);
        }
      });
      }
    }
  });
});

Now the problem is that, when i console.log kid.gameScores array I find my newly added score in it, but when I console.log(kids) then it doesn't get updated.现在的问题是,当我在 console.log child.gameScores 数组中找到我新添加的分数时,但是当我 console.log(kids) 时它没有得到更新。 In mongodb Atlas i cant find new game scores.在 mongodb Atlas 我找不到新的游戏分数。 But the problem is once a while they get entered into the database.但问题是它们有时会被输入数据库。 I m so confused as to why this problem is occurring.我很困惑为什么会出现这个问题。 I know that I m sending right data and it is being updated in kid.gameScores array but "Kid" doesn't get updated with new gamescore array.我知道我正在发送正确的数据,并且它正在 child.gameScores 数组中更新,但“Kid”没有更新为新的 gamescore 数组。

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

this is where im updating my arrays.这是我更新我的 arrays 的地方。 Any help.任何帮助。

If you want to make sure that the findOneAndUpdate successfully updated your record, you need to add { new: true } in the last parameter.如果要确保findOneAndUpdate成功更新了您的记录,则需要在最后一个参数中添加{ new: true } See here .这里

So you should update your codes to be所以你应该更新你的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM