node.js - 猫鼬不更新
问题描述
我最近在做一个端点,我遇到了一个问题:Mongoose 没有更新条目。
export const playersTurn = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
[...]
let symbol: 'X' | 'O';
room.roomPlayers.forEach((player) => {
if (player.id === id) symbol = player.symbol;
});
room.board[row][column] = symbol;
room.save();
res.status(201).json({
message: 'Your turn has been registered!',
status: 201,
});
} catch {
return next({
status: 500,
message: 'Unexpected error occured!',
});
}
};
这是架构,也是:
const roomSchema = new Schema(
{
[...]
board: [
[String, String, String],
[String, String, String],
[String, String, String],
],
[...]
},
{ timestamps: true }
);
有人可以解释一下,为什么这不起作用?提前致谢。
解决方案
您需要在代码中添加等待
await room.save();
推荐阅读
- excel - MS Excel:如何“展平”从公式创建的超链接?
- swift - 不允许使用 ARView 会话变量?
- javascript - jquery中继器中的CKEditor?
- python-3.x - 如何在 Google Cloud Composer 中安装 PYPI 包(Facebook-business、Google-cloud-secret-manager)
- html - 如何在三角形下给出阴影?
- javascript - Bootstrap col 不适用于整页 JS
- javascript - JQuery 过滤 - 按钮和搜索
- css - 通过 squarespace 更改我的主页滑块的亮度
- ruby-on-rails - 新字段适用于 localhost 但不适用于 Heroku
- c++ - 递归函数中的 CPP 向量连接