简体   繁体   English

ExpressJS:req.body 值以字符串“[object Object]”的形式出现

[英]ExpressJS: req.body value comes as a string “[object Object]”

I'm trying to get multer working on my backend to be able to handle files.我正在尝试让 multer 在我的后端工作以能够处理文件。 I'm sending (POST) from an Angular 9+ Front-End a FormData Object to my backend that looks like:我正在从 Angular 9+ 前端向我的后端发送(POST)一个 FormData Object,如下所示:

foo: 'test',
bar: { id: 'someId', name: 'someName' },
file: { SOMEFILE }

On my back-end I've got a server.js file:在我的后端,我有一个 server.js 文件:

const path = require('path');
const express = require("express");
const cors = require('cors');
const bodyParser = require("body-parser");
const mongoose = require('mongoose');

const app = express();

// SOME ROUTE IMPORTS -- Just the variable pointing to the route.


app.use(cors());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

mongoose.connect(uri, {
    useNewUrlParser: true, 
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB Connection Error:'));

// THERE I "USE" THE PREVIOUSLY IMPORTED ROUTES: app.use(routeX);

and the route file in question (let's call it routeX):和有问题的路由文件(我们称之为routeX):

const express = require('express');
const router = express.Router();
const multer = require('multer');

const MIME_TYPE_MAP = {
    'image/png': 'png',
    'image/jpeg': 'jpeg',
    'image/jpg': 'jpg',
    'application/pdf': 'pdf'
}

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        const isValid = MIME_TYPE_MAP[file.mimetype];
        let error = new Error("Invalid mimeType");
        if (isValid) {
            error = null;
        }
        cb(error, "docLibrary/equipment/");
    },
    filename: (req, file, cb) => {
        const dateNow = Date.now();
        const name = dateNow + '-' + file.originalname.toLowerCase().split(' ').join('_') + '-' + file.fieldname;
        const ext = MIME_TYPE_MAP[file.mimetype];
        cb(null,name + '.' + ext)
    }
});

const upload = multer({storage: storage});
const cpUpload = upload.fields([{name: 'proforma', maxCount: 1}, {name: 'technicalDetails', maxCount: 1}]);

router.post('/newEquipment', cpUpload, async (req, res, next) => {
    const url = req.protocol + '://' + req.get('host') + '/';
    let receivedEquip = req.body;
    console.log('receivedEquip\n');
    console.log(JSON.stringify(receivedEquip));
    console.log('\n');
}

The problem is that the console.log functions gives me the following JSON for req.body:问题是console.log函数给了我req.body的以下JSON:

"foo": 'test',
"bar": "[object Object]",

"bar" has become for some reason a String object that specifies the type of its value... I get the feeling that it's because of multer and the fact that I'm sending a FormData but I'm not sure where to look at here... Any ideas?由于某种原因,“bar”已成为 String object,它指定了其值的类型......我觉得这是因为 multer 以及我正在发送 FormData 但我不确定在哪里看在这里...有什么想法吗?

Ok, try doing this;好的,试试这个;

router.post('/newEquipment', cpUpload, async (req, res, next) => {
const url = req.protocol + '://' + req.get('host') + '/';
let receivedEquip = JSON.parse(JSON.stringify(req.body));
let uploadFile = req.files.file  // to get the file from the formData
console.log('receivedEquip\n');
console.log(receivedEquip);
console.log('\n');

}); });

Alright, so it was pretty dumb afterall... I'm not familiar with the FormData Object and by this point I wish there was a better way to send blob to the back end.好吧,毕竟这很愚蠢......我不熟悉 FormData Object ,此时我希望有更好的方法将 blob 发送到后端。 Not sure it exists, so for the moment, the workaround goes like this:不确定它是否存在,所以目前,解决方法如下:

ORIGIN OF THE PROBLEM:问题的根源:

As mentioned by our good friend VSCode - a FormData doesn't accept an object as a value.正如我们的好朋友 VSCode 所提到的 - FormData不接受 object 作为值。 Only Blobs and strings :只有Blobsstrings

Argument of type '{ test: string; secondTest: string; }' is not assignable to parameter of type 'string | Blob'.
  Object literal may only specify known properties, and 'test' does not exist in type 'Blob'.ts(2345)

SOLUTION解决方案

So all you have to do when appending to the FormData is stringify your JSON Object :因此,在附加到FormData时,您所要做的就是对您的JSON Object进行字符串化:

if (typeof currFormValue[key] === 'object') {
              const currObject = JSON.stringify(currFormValue[key])
              someFormDataObject.append(key, currObject);
            }

and on the server parse agains these fields to JSON like so:并在服务器上将这些字段再次解析为JSON ,如下所示:

Object.keys(req.body).map(reqKey => { if (reqKey === 'knowObject') { JSON.parse(req.body[reqKey]); } }

I'm sure there's a much better structure to work with out there but I know better than asking on SO which structure to use;)我确信那里有一个更好的结构可以使用,但我知道比问 SO 要使用哪种结构更好;)

Thanks all!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM