如何解析multer返回的javascript对象? [英] How can I parse a javascript object returned by multer?

查看:86
本文介绍了如何解析multer返回的javascript对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用multer快速处理csv文件的上传,然后逐行解析该文件.我可以将文件作为包含在req.body中的对象来获取,但是我无法对其进行解析,因为它现在似乎只是一个对象,而不再是一个csv字符串.如何分别从上传的文件中获取每一行? (应用程序/索引):

I am trying to use multer to handle upload of a csv file in express, then parse the file line by line. I am able to get the file as an object contained in req.body, but I am not able to parse it since it seems to now be just an object and no longer a csv string. How can I get each line from the uploaded file, individually? (app/index):

const csv = require('csvtojson');
const multer  = require('multer');
const upload = multer();

router.post('/distributor/:id/upload', upload.single(), function (req, res, 
next) {
  req.setTimeout(600000);
  console.log(req.body) 
  next()
}, function (req, res, next) {
    //calling req.body here returns an object with \r between each line from 
    file, but I cannot seem to parse each line
    res.end();
}) 

我试图在第二个函数内使用csvtojson,如下所示:

I tried to use csvtojson inside the second function, like this:

 csv()
.fromString(req.body.toString(UTF8))
.subscribe((csvLine)=>{
    console.log(csvLine); 
} 

但这只是尝试解析整个对象,而不是解析其中的每一行.这是multer返回的对象的片段:

But this just tries to parse the whole object, not each line inside of it. Here is a snippet of the object that is returned by multer:

{"UPC,Price,SKU,Title\r\n043171884536,1.17,538397,ORANGE YARN EGGS SIZE 4 - 
4 PK\r\n043171080044,1.39,942227,MIKE'S YELLOW/CORN GLO 
BAIT\r\n035011000046,1.98,161687,REPLACEMENT BRAKE 
PADS\r\n056389001503,1.79,41582,FIRE 
LIGHTERS\r\n087837005156,5.04,266320,PLATINUM GREEN 1/4LB SPOOL 
25#\r\n046295070045,1.54,604652,MIKE'S GARLIC GLO-SCENT 
OIL\r\n043171660161,1.02,126011,THREAD  RED 100'\r"} 

编辑**在我使用bodyParser之前.我在一个问题中看到,将bodyParser与multer一起使用将不起作用,因此我将其注释掉.现在,我没有得到正文对象,文件对象或文件对象.我尝试使用upload.none()并在请求中发送相同的文件,以查看是否可以通过错误消息"LIMIT_UNEXPECTED_FILE"来使它出错,但是没有成功,因此看起来multer甚至都无法识别该文件正在发送.我正在Postman中对此进行测试,将content-type设置为false,将正文作为二进制文件并附加一个文件.尝试以这种方式测试请求是否有问题?

Edit** Before I was using bodyParser. I saw in an issue that using bodyParser with multer will not work, so I commented it out. Now, I don't get a body object, or a file object, or a files object. I tried using upload.none() and sending the same file in the request, to see if I could get it to error out with the message "LIMIT_UNEXPECTED_FILE", but it did not, so it seems multer does not even recognize that a file is being sent. I am testing this in Postman, with the content-type set to false and the body as binary with a file attached. Is there something wrong with trying to test the request that way?

推荐答案

在multer文档提供的示例中,req.body对象将包含表单中的文本字段,而req.file将包含以下格式的文件:上传:

In the example provided by the multer documentation, the req.body object will contain the text fields from the form, and req.file will contain the file that was uploaded:

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

您是否尝试过使用file对象?类似于以下内容:

Have you tried using the file object? Something like the following:

router.post('/distributor/:id/upload', upload.single(), function (req, res, next) {
  req.setTimeout(600000);
  console.log(req.body)
  console.log(req.file)
  csv()
  .fromFile(req.file.path)
  .then((jsonObj)=>{
    console.log(jsonObj);
    //loop through array of objects here
  }) 
  next()
});

这篇关于如何解析multer返回的javascript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆