英文:
node.js - http request with status 200 but no response data?
问题
你好,我有一个mongoose数据库,我想根据账户ID检索数据,我正在使用一个中间件来验证提供ID的帐户,响应HTTP方法是200,但我在Postman上收到的数据为空。
async function getAccountDetails(authAccount) {
try {
const accId = authAccount._id;
const account = await Users.findById({ _id: accId });
console.log(account);
return account;
} catch (error) {
next(error);
}
}
router.get(
"/list",
authMiddleware,
async function _getallUsersByAccount(req, res, next) {
try {
const data = await getAccountDetails(req.authAccount);
console.log(data);
} catch (error) {
console.log("没有数据");
next(error);
}
}
);
英文:
hello i have a mongoose database and i want to retrieve data based on the account id, im using a middleware to verify the account which provides the id, the response http method is 200 but i receive no data on postman.
async function getAccountDetails(authAccount) {
try {
const accId = authAccount._id;
const account = await Users.findById({ _id: accId });
console.log(account);
return account;
} catch (error) {
next(error);
}
}
/**
router.get(
"/list",
authMiddleware,
async function _getallUsersByAccount(req, res, next) {
try {
const data = await getAccountDetails(req.authAccount);
console.log(data);
} catch (error) {
console.log("no bot");
next(error);
}
}
);
答案1
得分: 1
I think you are forgetting to send your response back.
在你的 router.get 函数中的某处,你需要返回数据。
尝试这样做:
router.get(
"/list",
authMiddleware,
async function _getallUsersByAccount(req, res, next) {
try {
const data = await getAccountDetails(req.authAccount);
console.log(data);
res.send(data);
} catch (error) {
console.log("没有机器人");
next(error);
}
}
);
我在 console.log(data) 后面添加了 res.send(data)。
英文:
I think you are forgetting to send your response back.
Somewhere in your router.get function you need to return data.
Try this:
router.get(
"/list",
authMiddleware,
async function _getallUsersByAccount(req, res, next) {
try {
const data = await getAccountDetails(req.authAccount);
console.log(data);
res.send(data);
} catch (error) {
console.log("no bot");
next(error);
}
}
);
I've added res.send(data) after the console.log(data).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论