Linked Questions

Popular Questions

Mongoose find0neAndUpdate property replace

Asked by At

quick explanation : It is supposed to be a game prematch, where both players have to confirm before it starts. If anyone leaves the game, the client will send player uuid and the server will look for it and remove his data from the game(database record). Then, the player who still in the game will always be Player1.

Question : When y try to replace player1 data with player2 data (same with SocketID) it says its undefined. How can I do that?

Sorry for my English and thanks for your help, I am a beginner.

 router.post('/newmatch/', matchCtrl.searchOrCreateMatch);  const mongoose = require('mongoose');


const matchSchema = new mongoose.Schema({
    matchId: String,
    player1: String,
    player2: String,
    listoP1: Boolean,
    listoP2: Boolean,
    socketID1: String,
    socketID2: String   
}); 
module.exports = mongoose.model("drawmatch",matchSchema);




const creatematch = require('../models/match');    
exports.RemovePlayerfromMatchprueba = function (req, res) {
        let uuid = req.body.uuid;
        creatematch.findOneAndUpdate({ player1: uuid }, { $set: { player1: player2, player2: null, listoP1: false, listoP2: false, socketID1: socketID2, socketID2: null, } }, function (err, response) {
            if(!response) {
                creatematch.findOneAndUpdate({ player2: uuid }, { $set: { player2: null, listoP1: false, listoP2: false, socketID2: null, } });
            }
            if (err) {
                return res.send(`Server error ${err}`)
            }
            else {
                res.send(response);
            }
        })
    }

Related Questions