我读了另一篇关于这个问题的帖子,但我无法弄清楚如何将其应用到我的情况(使用mongoose时node js Array.push()不起作用)
这是我的Mongoose异步populate函数,我想要做的是将一个值推入一个包含每个评论的最终数组中,稍后我会对其进行某些操作。然而,.push()不起作用,我总是得到一个空数组;根据我所读的内容,这与for循环是同步的,无法接受异步值有关,但我不确定该如何修复它
//空数组将被填充所有分数
const allReviewsRating = []
this.populate("reviews").then(async function (allReviews) {
for (const review of allReviews.reviews) {
allReviewsRating.push(review.rating)
}
// async.map(review, allReviews.reviews)
})
但即使我做类似这样的事情
const allReviewsRating = []
this.populate("reviews").then(async function (allReviews) {
allReviewsRating.push(`1`)
for (const review of allReviews.reviews) {
allReviewsRating.push(review.rating)
}
// async.map(review, allReviews.reviews)
})
最终的数组为空,这让我更加困惑
我想知道我需要做什么才能让我的数组不为空,并且实际上具有我想要的值
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我认为你需要检查一下,
this.populate()函数没有填充reviews,如果它填充了,那么问题可能出在allReviews.reviews,即所有评论中没有评论。其他所有功能都正常。我在我的系统上进行了测试。