用户 (1) 删除其帐户后,如何从用户 (2) 选项卡中删除用户 (1) 的收藏帖子?
How can I remove bookmarked posts of user (1) from user (2) tab after user (1) deletes his account?
在为具有几乎所有基本社交媒体操作(登录、注册、添加 post, 删除一个post, 删除账号, 关注用户...),
我目前遇到一个问题,在实现 post 书签功能后,我无法想出一个解决方案来从另一个用户的 post 书签页面中删除书签 post ,在第一个用户删除他的帐户之后。我将在下面提供我的代码:
(P. S. Bookmarks 是 User 模型中的一个数组。我还想提一下我最初为任务准备的步骤:
通过 ID 获取当前用户
然后获取这个用户创建的所有post,其中returns一个数组,所以我映射它得到每个Posts id
之后,我获取了应用程序中的所有用户,最初打算将每个用户的书签数组中的 post 与当前用户已创建。然后我将从每个用户的书签数组中提取这些相同的 posts。
--> 我认为我分析的逻辑是可以维护的,但它对我不起作用。这是下面的代码:
export const deleteUser = async (req, res) => {
试试{
let user = await User.findById(req.params.userId)
const userPosts = await Post.find({ creatorId: user._id })
const allUsers = await User.find()
const myPostsIds = userPosts.map((post) => post._id.toString())
//This is the section I've implemented for my task, but obviously
something isn't right
await Promise.all(
myPostsIds.forEach((id) =>
allUsers.map((user) => {
user.bookmarks.includes(id) &&
user.updateOne({ $pull: { bookmarks: id } })
})
)
)
await Post.deleteMany({ creatorId: user._id })
await user.remove()
res.status(200).json({
message: "Account has been deleted successfully!",
})
} catch (err) {
errorHandler(res, err)
}
}
如我的评论所述,您传递给 Promise.all 的值不是 Promise/array 异步函数的数组。
第二个错误在(当前)forEach
函数内 .map()
您没有在映射调用中返回任何内容。
所以应该这样做:
// first convert all ids to a promise
await Promise.all(myPostsIds.map(id => new Promise(resolve => {
// during this, await every test and update
return Promise.all(allUsers.map(user => new Promise(resolve => {
// if it includes the id, cast the update and then resolve
if (user.bookmarks.includes(id)) {
// if found, resolve the promise for this user after the change
user.updateOne({ $pull: { bookmarks: id } }).then(resolve)
} else {
// resolve directly if not found.
resolve()
}
// when all users are done for this id, resolve the Promise for the given id
}))).then(resolve)
})))
更容易阅读和更简短的方法是:
for (const id of myPostIds) {
for (const user of allUsers) {
if (user.bookmarks && user.bookmarks.includes(id)) {
await user.updateOne({ $pull: { bookmarks: id } });
}
}
}
在为具有几乎所有基本社交媒体操作(登录、注册、添加 post, 删除一个post, 删除账号, 关注用户...), 我目前遇到一个问题,在实现 post 书签功能后,我无法想出一个解决方案来从另一个用户的 post 书签页面中删除书签 post ,在第一个用户删除他的帐户之后。我将在下面提供我的代码: (P. S. Bookmarks 是 User 模型中的一个数组。我还想提一下我最初为任务准备的步骤:
通过 ID 获取当前用户
然后获取这个用户创建的所有post,其中returns一个数组,所以我映射它得到每个Posts id
之后,我获取了应用程序中的所有用户,最初打算将每个用户的书签数组中的 post 与当前用户已创建。然后我将从每个用户的书签数组中提取这些相同的 posts。 --> 我认为我分析的逻辑是可以维护的,但它对我不起作用。这是下面的代码:
export const deleteUser = async (req, res) => { 试试{
let user = await User.findById(req.params.userId) const userPosts = await Post.find({ creatorId: user._id }) const allUsers = await User.find() const myPostsIds = userPosts.map((post) => post._id.toString())
//This is the section I've implemented for my task, but obviously something isn't right
await Promise.all(
myPostsIds.forEach((id) =>
allUsers.map((user) => {
user.bookmarks.includes(id) &&
user.updateOne({ $pull: { bookmarks: id } })
})
)
)
await Post.deleteMany({ creatorId: user._id })
await user.remove()
res.status(200).json({
message: "Account has been deleted successfully!",
})
} catch (err) {
errorHandler(res, err)
}
}
如我的评论所述,您传递给 Promise.all 的值不是 Promise/array 异步函数的数组。
第二个错误在(当前)forEach
函数内 .map()
您没有在映射调用中返回任何内容。
所以应该这样做:
// first convert all ids to a promise
await Promise.all(myPostsIds.map(id => new Promise(resolve => {
// during this, await every test and update
return Promise.all(allUsers.map(user => new Promise(resolve => {
// if it includes the id, cast the update and then resolve
if (user.bookmarks.includes(id)) {
// if found, resolve the promise for this user after the change
user.updateOne({ $pull: { bookmarks: id } }).then(resolve)
} else {
// resolve directly if not found.
resolve()
}
// when all users are done for this id, resolve the Promise for the given id
}))).then(resolve)
})))
更容易阅读和更简短的方法是:
for (const id of myPostIds) {
for (const user of allUsers) {
if (user.bookmarks && user.bookmarks.includes(id)) {
await user.updateOne({ $pull: { bookmarks: id } });
}
}
}