Egoiistic Prince
Egoiistic Prince

Reputation: 17

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,

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. In mongodb Atlas i cant find new game scores. 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.

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

this is where i m updating my arrays. Any help.

Upvotes: 0

Views: 172

Answers (1)

Ahmad Shahab
Ahmad Shahab

Reputation: 46

If you want to make sure that the findOneAndUpdate successfully updated your record, you need to add { new: true } in the last parameter. See here.

So you should update your codes to be

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

Upvotes: 1

Related Questions