我在 Node JS Express 中有一個這樣編碼的 api 呼叫處理程式。
const express = require("express");
const router = express.Router();
const PostModel = require("../models/postModel");
const ProfileModel = require("../models/profileModel");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const uuid = require("uuid");
//please ignore unused dependencies because im using them in other api call handlers
router.post('/get-posts/', (req, res) => {
const { userId } = req.body;
PostModel.find((err, postData) => {
if(err){
return res.send({
success: "False",
message: "Error while getting posts"
});
}
const data = [];
postData.forEach(async (p, index) => {
const b64 = Buffer.from(p.post.image.data).toString('base64');
const mimeType = p.post.image.contentType;
const url = `data:${mimeType};base64,${b64}`;
var ddd = await ProfileModel.findOne({
userId: p.authorId
}, async (e, d) => {
if(e){
console.log(e);
}
}).clone();
let dpMimeType = ddd.profilePic.contentType;
let dpUrl = `data:${dpMimeType};base64,${b64}`;
const obj = {
authorId: p.authorId,
postId: p.postId,
dp: dpUrl,
post: {
authorName: p.post.authorName,
creationDate: p.post.creationDate,
heading: p.post.heading,
image: url,
message: p.post.message,
likes: p.post.likes,
dislikes: p.post.dislikes,
hearts: p.post.hearts,
comments: p.post.comments,
likedUsers: p.post.likedUsers,
dislikedUsers: p.post.dislikedUsers,
heartedUsers: p.post.heartedUsers
}
};
data.push(obj);
});
if(postData.length > 0){
return res.status(200).send({
posts: data
});
} else {
return res.status(200).send({
message: "No posts found for this user"
});
}
});
});
現在我在這里做的是從資料庫中獲取 PostModel 中的所有帖子。然后我遍歷每個帖子,然后查詢該帖子的 authorId 并將其傳遞給 ProfileModel 以取回作者的 dp,將其附加到我的回應正文并將其發送回我的 React 前端框架,在那里我可以訪問相同的內容并顯示作者的 dp 以及作者的帖子。
但是這里發生的問題是,我從 api 呼叫中得到以下回應:-
{"posts":[]}
即回傳空白回應。但是我的節點服務器也沒有因為一些錯誤而中斷,任何地方都沒有錯誤被注銷。它運行順利,但是盡管資料庫中的兩個集合中有足夠的檔案,但沒有回傳任何回應。所以它不是資料庫為空的問題。
我不知道該怎么做。非常感謝任何和所有幫助。
uj5u.com熱心網友回復:
forEach不適合與 一起使用async await,請嘗試以下邏輯:
router.post('/get-posts/', async (req, res) => {
try {
const { userId } = req.body;
const postData = await PostModel.find({});
if (postData.length === 0) {
return res.status(400).send({
message: 'No posts found for this user',
});
}
const data = [];
for (const p of postData) {
const b64 = Buffer.from(p.post.image.data).toString('base64');
const mimeType = p.post.image.contentType;
const url = `data:${mimeType};base64,${b64}`;
var ddd = await ProfileModel.findOne({
userId: p.authorId,
});
let dpMimeType = ddd.profilePic.contentType;
let dpUrl = `data:${dpMimeType};base64,${b64}`;
const obj = {
authorId: p.authorId,
postId: p.postId,
dp: dpUrl,
post: {
authorName: p.post.authorName,
creationDate: p.post.creationDate,
heading: p.post.heading,
image: url,
message: p.post.message,
likes: p.post.likes,
dislikes: p.post.dislikes,
hearts: p.post.hearts,
comments: p.post.comments,
likedUsers: p.post.likedUsers,
dislikedUsers: p.post.dislikedUsers,
heartedUsers: p.post.heartedUsers,
},
};
data.push(obj);
}
return res.status(200).send({
posts: data,
});
} catch (err) {
res.status(500).send({
success: 'False',
message: 'Error while getting posts',
});
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/525478.html
