node.js – 发送HTTP请求,状态码为200,但没有响应数据?

huangapple go评论159阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2023年6月13日 05:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定