【问题标题】:Exporting and requiring an async function error导出并要求异步函数错误
【发布时间】:2019-01-15 19:58:34
【问题描述】:

我在一个名为 resources.js 的文件中导出一个异步函数,如下所示:

//resourcess.js
module.exports = function(arg) {
    let do_stuff = async (arg) => {
    ...
}

然后我需要这样的 routes.js 文件:

let importedFunc = require('./resourcess.js');

最后我像这样在 routes.js 中使用它:

app.post('/post', function(req, res) {
        var a2 = req.body.a1;
        importedFunc(a2).then(result => {
            console.log(result);
        res.render('index.ejs');
        }).catch(err => {
            console.log(err);
            res.render('index.ejs');
        })
    });

这是我收到的错误消息:

TypeError: Cannot read property 'then' of undefined

我无法理解我做错了什么......

【问题讨论】:

    标签: node.js module async-await export require


    【解决方案1】:

    如果您没有调用 do_stuff 并返回承诺,那么导出的函数实际上并没有返回承诺:

    //resourcess.js
    module.exports = function(arg) {
        let do_stuff = async (arg) => {
          // something should be done inside this function
          let data = await somethingThatReturnsData(arg);
          return data;
        };
        return do_stuff(arg);
    }
    

    但是从它的使用方式来看,我认为您想要执行以下操作:

    //resourcess.js
    // see that async is on the actual exported function
    module.exports = async function(arg) {
      let data = await somethingThatReturnsData(arg);
      // do stuff to data
      return data;
    };
    

    【讨论】:

      【解决方案2】:

      您的resourcess.js 文件是一个包装异步函数的函数。

      //resourcess.js
      module.exports = function(arg) {
          let do_stuff = async (arg) => {
          ...
      }
      

      你没有先调用导入的函数,所以里面的异步函数还不存在。

      app.post('/post', function(req, res) {
              var a2 = req.body.a1;
              importedFunc(a2).then(result => {
                  console.log(result);
              res.render('index.ejs');
              }).catch(err => {
                  console.log(err);
                  res.render('index.ejs');
              })
          });
      

      要更正它,只需将其重写为importedFunc()(a2).then

      如果您想像现在一样使用它,请像这样重做resourcess.js 实现:

      //resourcess.js
      module.exports = async function do_stuff(arg) {
          ...
      }
      

      //resourcess.js
      module.exports = async arg => {
          ...
      }
      

      ... 是 do_stuff 函数中的代码。

      【讨论】:

        【解决方案3】:

        如果你打算使用async/await,那就坚持下去,尽量不要将它与承诺混为一谈。

        app.post('/post', async function(req, res) {
            try {
                var a2 = req.body.a1;
                var result = await resimportedFunc(a2);
                console.log(result);
                res.render('index.ejs');
            } catch(err){
                console.log(err);
                res.render('index.ejs');
            }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-18
          • 1970-01-01
          • 2019-10-28
          • 1970-01-01
          • 2015-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多