gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'then' of undefined on Promise in Express route

转载 作者:行者123 更新时间:2023-12-03 03:49:01 26 4
gpt4 key购买 nike

我的 MEEN 应用程序中有一条路线,它接受文件上传,然后将数据传递到解析文件并将其存储在数组中的辅助模块,一旦此辅助模块完成,我想将其传递给另一个模块然后将处理返回的数组。由于某种原因,即使我在帮助程序末尾有一个 return 语句,then 方法也是未定义的。

路线:

router.route('/tools/sku/reactivate').post(upload.single('csvdata'),function(req,res){
console.log('handing request over to helper.csv');
csv.reader(req,res).then(sku.reactivate(data));
});

csv.reader:

var csv = require('csv-parse');
var multer = require('multer');
var fs = require('fs');

module.exports.reader = function(req,res){
//define array for holding csv data in this case skus
const csvArray = [];
//max number of columns in csv
const maxColumns = parseInt(req.body.maxColumns);
//create an array of column headers to check chunks against so we dont parse headers
let columnHeader = req.body.moduleTarget.split(',');
//loopThrough Array to create arrays inside container array for each column
for(var i = 0; i < maxColumns; i++){
csvArray.push([]);
}
//define filesystem readstream from uploaded file
let readStream = fs.createReadStream(req.file.path).pipe(csv());
//push csv data to array ignoring headers to csvArray
readStream.on('data', function(chunk){
//get number of keys in the dataChunk
let chunkLength = Object.keys(chunk).length;
//check column count on csv if greater than expected throw error
if(chunk[maxColumns]){
throw '[ERROR] More columns than expected in CSV, please fix and try again';
}else{
//loop through chunk keys and store each one in csvArray by index
for(var i = 0; i < chunkLength; i++){
//if chunk at this index doesnt equal column header and chunk at this index exists push to array
if(chunk[i] !== columnHeader[i] && chunk[i]){
csvArray[i].push(chunk[i]);
}
}
}
});
//error handling
readStream.on('error',function(err){
console.log('Error while reading file stream '+ err);
res.json({message:err,errorType:'1'});
});
readStream.on('end',function(){
console.log('finished reading csv returning array back to router to process next middleware');
return csvArray;
});
}

我在 readStream 结束监听器上收到 console.log 消息后立即收到错误

最佳答案

来自 Express 网站。 https://expressjs.com/en/advanced/best-practice-performance.html

TL;博士

尝试将阅读器模块的内容包装在 promise 中并返回 promise 。

...但是,有两个注意事项:所有异步代码都必须返回 Promise(发射器除外)。如果特定库不返回 Promise,请使用 Bluebird.promisifyAll() 等辅助函数转换基础对象。事件发射器(如流)仍然可能导致未捕获的异常。因此,请确保您正确处理错误事件;例如:

app.get('/', wrap(async (req, res, next) => {
let company = await getCompanyById(req.query.id)
let stream = getLogoStreamById(company.id)
stream.on('error', next).pipe(res)
}))

@示例:::

module.exports.reader = function(req,res){
return new Promise((resolve, reject)=>{
//define array for holding csv data in this case skus
const csvArray = [];
//max number of columns in csv
const maxColumns = parseInt(req.body.maxColumns);
//create an array of column headers to check chunks against so we dont parse headers
let columnHeader = req.body.moduleTarget.split(',');

...
//error handling
readStream.on('error',function(err){
console.log('Error while reading file stream '+ err);
res.json({message:err,errorType:'1'});
});
readStream.on('end',function(){
console.log('finished reading csv returning array back to router to process next middleware');
return csvArray;
});
})
}

关于javascript - 类型错误 : Cannot read property 'then' of undefined on Promise in Express route,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45247247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com