首页 > 解决方案 > 我如何在 NodeJS 中的这个 async module.exports 中看到结果

问题描述

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

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

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

我想记录 res 看看发生了什么。像这样的东西:

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

  console.log(res)

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

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

hello
world

标签: javascriptnode.jsgoogle-cloud-platform

解决方案


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

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

尝试类似:

请求文件.js

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

测试文件.js

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

推荐阅读