Nodejs 话题收藏计数器bug
Nodejs 话题收藏计数器bug
用户收藏某个话题后,删除该话题,用户的话题收藏计数器未减一。
3 回复
Nodejs 话题收藏计数器bug
在使用Node.js开发一个讨论区应用时,我们可能会遇到这样一个问题:当用户收藏某个话题后,如果该话题被删除,用户的收藏计数器却没有正确地减少。这会导致数据不一致的问题。
问题描述
假设我们有一个话题收藏功能,用户可以收藏他们感兴趣的话题。当用户收藏话题时,收藏计数器应该增加;当用户取消收藏或话题被删除时,收藏计数器应该相应地减少。
然而,在某些情况下,当某个话题被删除后,用户的收藏计数器没有更新,导致计数器与实际的收藏状态不一致。
示例代码
以下是一个简化的示例,展示了如何实现话题收藏和计数器的功能:
const express = require('express');
const mongoose = require('mongoose');
// 连接到MongoDB数据库
mongoose.connect('mongodb://localhost:27017/discussionForum', { useNewUrlParser: true, useUnifiedTopology: true });
// 定义模型
const UserSchema = new mongoose.Schema({
username: String,
collections: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Topic' }]
});
const TopicSchema = new mongoose.Schema({
title: String,
users: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
});
const User = mongoose.model('User', UserSchema);
const Topic = mongoose.model('Topic', TopicSchema);
// 添加收藏
app.post('/collections/:topicId', async (req, res) => {
const userId = req.user._id;
const topicId = req.params.topicId;
try {
// 更新用户收藏列表
await User.findByIdAndUpdate(userId, { $push: { collections: topicId } });
// 更新话题收藏者列表
await Topic.findByIdAndUpdate(topicId, { $push: { users: userId } });
res.status(200).send({ message: '收藏成功' });
} catch (err) {
res.status(500).send({ error: '收藏失败' });
}
});
// 删除话题
app.delete('/topics/:topicId', async (req, res) => {
const topicId = req.params.topicId;
try {
// 删除话题
await Topic.findByIdAndDelete(topicId);
// 更新所有收藏该话题的用户收藏列表
await User.updateMany(
{ collections: topicId },
{ $pull: { collections: topicId } }
);
res.status(200).send({ message: '话题删除成功' });
} catch (err) {
res.status(500).send({ error: '话题删除失败' });
}
});
解决方案
上述代码中,话题被删除后,需要更新所有收藏该话题的用户的收藏列表。这可以通过$pull
操作符来实现,确保从用户的收藏列表中移除相应的话题ID。
通过这种方式,我们可以确保话题被删除后,用户的收藏计数器能够正确地减少,从而避免数据不一致的问题。
请详细描述,前端,后端,数据库,报错信息等等
Node.js 话题收藏计数器 Bug
描述
当用户收藏某个话题后,如果删除该话题,用户的收藏计数器没有正确地减少。这可能是由于在删除话题时没有同步更新用户的收藏记录。
解决方案
假设我们使用MongoDB作为数据库,并且使用Mongoose作为ODM(对象文档映射)工具。
- 定义数据模型:
const mongoose = require('mongoose');
const TopicSchema = new mongoose.Schema({
title: String,
// 其他字段
});
const UserSchema = new mongoose.Schema({
username: String,
favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Topic' }]
// 其他字段
});
const Topic = mongoose.model('Topic', TopicSchema);
const User = mongoose.model('User', UserSchema);
- 添加收藏逻辑:
async function addToFavorites(userId, topicId) {
try {
const user = await User.findById(userId);
if (!user.favorites.includes(topicId)) {
user.favorites.push(topicId);
await user.save();
}
} catch (err) {
console.error(err);
}
}
- 删除话题时更新用户收藏计数器:
async function deleteTopic(topicId) {
try {
const topic = await Topic.findByIdAndDelete(topicId);
if (topic) {
const users = await User.find({ favorites: topicId });
for (const user of users) {
user.favorites.pull(topicId);
await user.save();
}
}
} catch (err) {
console.error(err);
}
}
示例
假设有一个用户 userId
和一个话题 topicId
:
// 用户收藏话题
await addToFavorites(userId, topicId);
// 删除话题
await deleteTopic(topicId);
总结
确保在删除话题时,从所有用户的收藏列表中移除该话题,以保持收藏计数器的准确性。上述代码展示了如何实现这一点。