gpt4 book ai didi

javascript - 我如何控制错误, Mongoose

转载 作者:行者123 更新时间:2023-12-03 00:02:59 24 4
gpt4 key购买 nike

这是一场私有(private)比赛,游戏有 2 名玩家,名字和密码,如果你想加入,你必须匹配他们。

问题是:我如何控制匹配或名称不匹配或游戏已满的情况,以便我可以向客户端发送不同的消息?如果有人可以向我展示另一篇有用的帖子或一些可以帮助我的文档链接,我将非常高兴。

谢谢

exports.joinPrivateMatch = (req, res) =>{
let gamePass = req.body.password;
let gameName = req.body.name;
let playerid = req.body.playerid;
let socketPID = req.body.socketid;

creatematch.findOneAndUpdate({player2: null, name: gameName, password: gamePass}, {
player2: playerid,
socketID2: socketPID
}, {new:true}, function(err, result){
if(result){
res.send(result);
ioConnection.to(`${result.socketID1}`).emit('addplayer2', result);
ioConnection.to(`${result.socketID1}`).emit('matchfull', /*{ScreenReady: true} */);
ioConnection.to(`${result.socketID2}`).emit('matchfull', /*{ScreenReady: false} */);
}
if(err){
return res.send('something went wrong', err)
}
else{
console.log('Contraseña incorrecta o partida llena')
ioConnection.to(`${socketPID}`).emit('sadsmiley')
}
})

我写了这样的东西:

 if(name != gameName || password != gamePass){
console.log("name or password doesnt match")
ioConnection.to(`${socketPID}`).emit('findError1')
}

但是只要我没有得到响应,名称和密码就会不确定。

最佳答案

您可以使用 Express 中间件来实现此目的...请参阅Using Express Middleware

var app = express()
var router = express.Router()

// a middleware function with no mount path. This code is executed for every request to the router
router.use(function (req, res, next) {
//here implement code for matching gameName & Password
//if not match, return authentication error response
//else pass the control to the next function
next()
})

关于javascript - 我如何控制错误, Mongoose ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55092526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com