导出并要求异步函数错误
Exporting and requiring an async function error
我在名为 resourcess.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
我不明白我做错了什么....
如果你想要 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');
}
});
如果您没有调用 do_stuff
并返回 promise,那么导出的函数实际上并没有返回 promise:
//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;
};
您的 resourcess.js
文件是一个包装异步函数的函数。
//resourcess.js
module.exports = function(arg) {
let do_stuff = async (arg) => {
...
}
你没有先调用导入函数,所以里面的async函数还不存在
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 函数中的代码。
我在名为 resourcess.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
我不明白我做错了什么....
如果你想要 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');
}
});
如果您没有调用 do_stuff
并返回 promise,那么导出的函数实际上并没有返回 promise:
//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;
};
您的 resourcess.js
文件是一个包装异步函数的函数。
//resourcess.js
module.exports = function(arg) {
let do_stuff = async (arg) => {
...
}
你没有先调用导入函数,所以里面的async函数还不存在
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 函数中的代码。