node.js - Mongoose findOneAndUpdate 在更新文档后偶尔会返回 null
问题描述
我正在使用 Socket.IO 来接收事件。我执行以下代码以使用新参与者更新目标。大多数情况下,这很有效,但是,有时即使文档已成功更新,该函数也会返回 null 目标。
socket.on('joinGroup', function(goalID, userName) {
if (goalID == null || userName == null) {
return socket.error("Missing some required field")
}
Goal.findOneAndUpdate({_id: goalID, 'participants.userID': {$ne : socket.uid}, 'participants.userName': {$ne : userName}}, {$push: {participants: {userID: socket.uid, userName: userName, streak: 0}}}, {new: true}, (err, goal) => {
if (err) {
return socket.error(error)
} else {
if (goal == null) {
//Means something is wrong, don't send
console.log("Goal is null")
return socket.emit('customError', "Unable to join goal. Please try again.")
}
else {
sendMessage(socket.uid, userName + " joined the goal.", goalID, 'joined', userName, true)
return socket.emit('goals', {goals: [goal]})
}
}
}).catch(function(error) {
return console.log(error);
});;
});
有谁知道为什么即使在文档成功更新后也会出现目标 == null ?
解决方案
推荐阅读
- javascript - 如何使用 React.js 将 state 属性传递给构造函数中的子组件?
- tensorflow - 我没有让 keras fit_generator 与混合(图像和数字)输入一起工作
- html - 如何逐个移动元素
- python - 上下文不打印 Django
- r - error en METRIC.EB .local(x, …) 中的错误:RasterLayer 没有 NA 像元(要计算距离)
- svg - Highcharts Scrollbar向上箭头在点击时抛出错误
- python - 如何在给定文件的给定行号处附加时间?
- spring-boot - spring-boot application.properties 文件可以使用 log4j2.xml 配置吗?
- spring-integration - 春季集成:SimpleAsyncTaskExecutor
- cisco-ios - Brocade NetIron CER 2024C BGP 配置问题