我正在使用 Node JS 和 Express JS,這是我的控制器代碼:
const UserComment = require("../model/UserComment");
router.post("/get/comments", async (request, response) =>{
try{
let currentUserID = request.body.userID;
let myUserComment = await UserComment.find({userID: currentUserID});
let friendsCommentsArray = [ ...myUserComment];
let friendsComments = await axios.post(`http://localhost:5000/router/accounts/account/following/list`, {userID: currentUserID})
.then((resp) => {
resp.data.message.map((parentArrayOfArray) =>{
parentArrayOfArray.map((friendID) =>{
let friendsCommentsToLookUp = UserComment.find({userID: friendID})
friendsCommentsToLookUp.then((commentsArray) =>{
commentsArray.map((comment) =>{
if(String(comment.userID) === friendID){
friendsCommentsArray.push(comment);
}else{
console.log("no")
}
})
});
});
});
}).catch((err) =>{
console.log("err: ", err);
throw err;
});
return response.status(200).json({message: friendsPostsArray});
}catch(err){
return response.status(400).json({message: `${err}`});
}
});
FriendsCommentsArray,當我 console.log 時,我可以看到資料,但是當我回傳它時,它是空的。有什么問題,為什么它是空的,即使我將每個評論都推送到friendsCommentsArray。
但是,回傳的friendsCommentsArray 是空的。如何解決這個問題?
謝謝。
uj5u.com熱心網友回復:
為了await Promise.all()作業,你需要回報承諾
return axios.get(`http://localhost:5000/comments/by/post/${post._id}`)
uj5u.com熱心網友回復:
一般在使用await的時候不需要使用.then()。您的問題是您的內部.map()正在使用friendsCommentsToLookUp.then(),但是在您繼續撰寫代碼之前,沒有任何事情等待這些承諾解決。有人可能認為您可以await承諾friendsCommentsToLookUp,但這不會起作用,因為不會等待對地圖回呼的呼叫。
洗掉.then()'s 使這更容易使用:
const resp = await axios.post(`http://localhost:5000/router/accounts/account/following/list`, {userID: currentUserID});
const message = resp.data.message;
for(const parentArrayOfArray of message) {
for(const friendID of parentArrayOfArray) {
const commentsArray = await UserComment.find({userID: friendID});
for(const comment of commentsArray) {
if(String(comment.userID) === friendID){
friendsCommentsArray.push(comment);
}
}
}
}
以上for..of允許我們暫停移動到 for 回圈的下一次迭代,直到 for 回圈的當前迭代中的 Promise 已經解決。即:它是順序的(注意:如果您嘗試使用.forEach()or執行此操作.map(),您的代碼將在您的 Promise 解決之前直接進入回圈之后的部分)。雖然,你所追求的不需要是連續的。我們可以創建一個 Promise 陣列,將Promise.all()其傳遞給我們可以等待并行解決。下面我展示了一種不同的方法.flatMap()來創建我們可以并行等待的 Promise 陣列Promise.all():
const resp = await axios.post(`http://localhost:5000/router/accounts/account/following/list`, {userID: currentUserID});
const message = resp.data.message;
const promises = message.flatMap(parentArr => parentArr.map(async friendID => {
const commentsArray = await UserComment.find({userID: friendID});
return commentsArray.filter(comment => String(comment.userID) === friendID);
}));
const nestedComments = await Promise.all(promises);
const friendsCommentsArray = [...myUserComment, ...nestedComments.flat()];
uj5u.com熱心網友回復:
而不是 push 嘗試連接陣列,讓我知道它是否有效。
friendsCommentsArray = [...friendsCommentsArray , {...comment}];
// insted of
friendsCommentsArray.push(comment);
當您不想從 map 陳述句中回傳一個新陣列時,也可以嘗試使用 forEach 而不是 map()。
map 方法與 forEach 方法非常相似——它允許您為陣列的每個元素執行一個函式。但不同的是,map 方法使用此函式的回傳值創建了一個新陣列。map 通過對源陣列的每個元素應用回呼函式來創建一個新陣列。由于 map 不會改變源陣列,我們可以說它是一個不可變的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494112.html
標籤:javascript 节点.js 表示 猫鼬 异步等待
