是什么导致我的请求超时?
What is causing my request to timeout?
我正在尝试创建一个允许用户上传 csv 文件的路由,然后将文件异步解析为 Json 并为每一行实例化一个模型。我曾尝试使用 promises 来这样做,但函数请求一直超时,我看不到它在哪里中断。这是代码(/app/index):
const csv=require('csvtojson');
const multer = require('multer');
const upload = multer().single();
router.post('/distributor/:id/upload', (req,res) => {
return new Promise((resolve, reject) => {
upload(req,res,function(err){
if(err !== null) return reject(err);
resolve();
});
})
.then((req, res) => {
return csv()
.fromString(req.body.toString('utf8'))
.on('json', (item) => {
item.distributor_id = req.params.id
Product
.forge(item.body)
.save()
.then((product) => {
res.json({id: product.id});
})
.catch((error) => {
console.error(error);
return res.sendStatus(500);
})
})
.on('done', () => {
console.log('done parsing');
resolve();
});
})
})
这是我 post 将文件发送到此路由时 heroku 日志的输出:
(node:49) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 2): undefined
2018-08-09T03:57:17.240599+00:00 app[web.1]: (node:49) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In the
future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.
2018-08-09T03:57:47.205596+00:00 heroku[router]: at=error code=H12
desc="Request timeout" method=POST path="/api/distributor/1/upload" host=fba-
prof-prods.herokuapp.com request_id=40e1864b-fa71-49aa-8fdf-cedb1752edef
fwd="73.92.68.83" dyno=web.1 connect=1ms service=30284ms status=503 bytes=0
protocol=https
如果您能指出任何 resources/examples 正确完成类似操作(处理上传和异步解析大型 csv 文件)的地方,我也将不胜感激。我不知道该怎么做,似乎找不到任何好的资源!谢谢
你需要做这样的事情:
function uploadAsync(req,res){
return new Promise((resolve, reject) => {
upload(req,res,function(err){
if(err !== null) return reject(err);
resolve();
});
});
}
注意 resolve()
。这是这里的关键,因为您没有对该 Promise 做任何事情。
我正在尝试创建一个允许用户上传 csv 文件的路由,然后将文件异步解析为 Json 并为每一行实例化一个模型。我曾尝试使用 promises 来这样做,但函数请求一直超时,我看不到它在哪里中断。这是代码(/app/index):
const csv=require('csvtojson');
const multer = require('multer');
const upload = multer().single();
router.post('/distributor/:id/upload', (req,res) => {
return new Promise((resolve, reject) => {
upload(req,res,function(err){
if(err !== null) return reject(err);
resolve();
});
})
.then((req, res) => {
return csv()
.fromString(req.body.toString('utf8'))
.on('json', (item) => {
item.distributor_id = req.params.id
Product
.forge(item.body)
.save()
.then((product) => {
res.json({id: product.id});
})
.catch((error) => {
console.error(error);
return res.sendStatus(500);
})
})
.on('done', () => {
console.log('done parsing');
resolve();
});
})
})
这是我 post 将文件发送到此路由时 heroku 日志的输出:
(node:49) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 2): undefined
2018-08-09T03:57:17.240599+00:00 app[web.1]: (node:49) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In the
future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.
2018-08-09T03:57:47.205596+00:00 heroku[router]: at=error code=H12
desc="Request timeout" method=POST path="/api/distributor/1/upload" host=fba-
prof-prods.herokuapp.com request_id=40e1864b-fa71-49aa-8fdf-cedb1752edef
fwd="73.92.68.83" dyno=web.1 connect=1ms service=30284ms status=503 bytes=0
protocol=https
如果您能指出任何 resources/examples 正确完成类似操作(处理上传和异步解析大型 csv 文件)的地方,我也将不胜感激。我不知道该怎么做,似乎找不到任何好的资源!谢谢
你需要做这样的事情:
function uploadAsync(req,res){
return new Promise((resolve, reject) => {
upload(req,res,function(err){
if(err !== null) return reject(err);
resolve();
});
});
}
注意 resolve()
。这是这里的关键,因为您没有对该 Promise 做任何事情。