【问题标题】:How can i see the result in this async module.exports in NodeJS我如何在 NodeJS 中的这个 async module.exports 中看到结果
【发布时间】:2021-11-01 18:29:18
【问题描述】:

我的代码中有这个模块需要调试。它在一个文件中,我们可以调用 GCP 云调度程序调用的 test.js

const run = require("../../run");

module.exports = async (req, res) => {
  await run(false);
  res.send("done");
};

我想记录资源以查看发生了什么。像这样的:

console.log('hello');
module.exports = async (req, res) => {
  await run(false);

  console.log(res)

  res.send("done");
};
console.log('world');

但我没有得到那个console.log。我会得到

hello
world

【问题讨论】:

  • 什么意思?
  • 提供更多日志
  • 更新了问题,抱歉
  • 你怎么知道导出的函数实际上被调用了?好像不是。 (或者await run(false) 没有完成,所以你永远不会到达console.log 行)
  • 我很确定console.log(res) 不会在helloworld 之间记录。因为异步代码不会作为同步代码运行。 :)

标签: javascript node.js google-cloud-platform


【解决方案1】:

你会首先看到“Hello”“World”,因为当你在某个文件中导入模块时它会打印出来。 console.logs 在模块“全局范围”中!!

我没有给你代码,但我觉得你没有调用函数,只是导出!

尝试类似:

requestfile.js

const run = require('../../run');
module.exports = async (req, res) => {
    const resp = await run(false);
    console.log(resp)
    res.send("done");
};

testfile.js

const req = require('./requestfile');
async function main () {
  await req();
}
main();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-16
    • 2017-05-21
    • 2019-01-02
    • 2017-10-07
    • 2013-08-14
    • 1970-01-01
    • 2021-02-02
    • 2014-06-12
    相关资源
    最近更新 更多