这段代码像它应该工作一样工作,但是在第五个 GET 请求之后,它会在后端执行它应该做的事情(将数据存储在 db 中),但它没有在服务器上记录任何内容,也没有任何更改在前端(reactjs)
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const User = require('./login').User;
mongoose.connect('mongodb://localhost:27017/animationsdb');
router.get('/', async(req, res) => {
await User.findOne({ username: req.query.username }, (err, result) => {
if (result) {
// when user goes to his profile we send him the list of animations he liked
// list is stored in array at db, field likedAnimations
res.send({ animationList: result.likedAnimations });
console.log("Lajkovane animacije:", result.likedAnimations);
} else {
console.log("no result found");
res.sendStatus(404)
}
});
});
router.put('/', async(req, res) => {
console.log("username:", req.body.username);
console.log("link:", req.body.link);
// if animation is already liked, then dislike it
// if it's not liked, then store it in db
const user = await User.findOne({ username: req.body.username });
if (user.likedAnimations.indexOf(req.body.link) === -1) {
user.likedAnimations.push(req.body.link);
} else {
user.likedAnimations = arrayRemove(user.likedAnimations, user.likedAnimations[user.likedAnimations.indexOf(req.body.link)]);
}
user.save();
});
function arrayRemove(arr, value) {
return arr.filter((item) => {
return item != value;
});
}
module.exports = router;
对于前五个请求,我得到以下输出:
Liked animations: ["/animations/animated-button.html"]
GET /animation-list/?username=marko 200 5.152 ms - 54
Liked animations: ["/animations/animated-button.html"]
GET /animation-list/?username=marko 304 3.915 ms - -
在那之后,我在服务器控制台上没有任何输出,前端也没有任何变化,直到我刷新页面,即使数据库操作仍然有效并且数据被保存。
答案 0 :(得分:1)
您似乎遇到了一些问题。首先,这个请求处理程序没有正确编码来处理错误,因此它使请求处于挂起状态,不发送响应,连接将保持挂起状态,直到客户端最终超时。其次,您可能有某种数据库并发使用错误,这是这里的根本问题。第三,您没有在数据库中正确使用 await
。您要么使用 await
,要么将回调传递给您的数据库,而不是两者都使用。您需要解决所有这三个问题。
解决第一和第三个问题:
router.get('/', async(req, res) => {
try {
let result = await User.findOne({ username: req.query.username };
if (result) {
console.log("Liked animations:", result.likedAnimations);
res.send({ animationList: result.likedAnimations });
} else {
console.log("no database result found");
res.sendStatus(404);
}
} catch(e) {
console.log(e);
res.sendStatus(500);
}
});
对于第二个问题,您提到的特定数据库错误似乎是数据库内部的某种并发/锁定问题,由您的代码执行的数据库操作序列触发。您可以在 discussion here 中阅读有关该错误的更多信息。由于您向我们展示的代码仅显示了一个读取操作,因此我们需要查看更大的相关代码上下文,包括与写入数据库的此操作相关的代码,以便能够提供有关如何修复的任何想法此问题的根本原因。
这里我们看不到整个流程,但是您需要在数据库中使用原子更新操作。您展示的 PUT 处理程序是立即竞争条件。在多客户端数据库中,您不会获得值,对其进行修改然后将其写回。这是竞争条件的机会,因为其他人可以在您坐在他们手中时修改该值。当您随后修改您持有的值时,您会覆盖其他客户刚刚所做的更改。这是一个竞争条件。相反,您可以使用原子操作直接在一次数据库调用中更新操作,或者使用事务将多步操作变成安全操作。
我建议您阅读 this article 以了解 mongodb 中的原子操作。而且,您可能想使用 .findAndModify()
之类的东西,以便您可以在一次原子操作中查找和更改数据库中的项目。如果你搜索“mongodb 中的原子操作”,还有很多关于这个主题的文章。