将身体分析checkbox表示为数组

我很新的节点/expression,并试图创build一个简单的表单,可以发布数据到Mongo数据库。

当我尝试添加一些checkbox并将值保存到一个数组时,我挣扎着。 例如

我的表单看起来像这样。

<form action="/addProduct" method="POST" encType="multipart/form-data"> <div> <label for="product-name">Name:</label> <input type="text" id="product-name" name="product_name"> </div> <div> <label for="product-price">Price:</label> <input type="text" id="product-price" name="product_price"> </div> <div> <label for="description">Description:</label> <textarea id="description" name="description"></textarea> </div> <input type="checkbox" name="categories" value="bedroom">Bedroom<br> <input type="checkbox" name="categories" value="kitchen">Kitchen<br> <input type="file" name="sampleFile" /> <div class="button"> <button type="submit">Submit</button> </div> </form> 

和server.js看起来像这样

 var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var MongoClient = require('mongodb').MongoClient; var db; app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); MongoClient.connect('xxxxxxxxxxxxxx', function (err, database) { if (err) return console.log(err); db = database; app.listen(3000, function () { console.log('listening on 3000'); }); }); app.post('/addProduct', function (req, res) { db.collection('products').save(req.body, function (err, result) { if (err) return console.log(err); console.log('saved to database'); res.redirect('/'); }); }); 

我无法弄清楚如何在req.body的对象中包含一个类别数组。 我希望它显示为

 { product_name: 'light', product_price: '100', description: 'its a light', categories: [ 'bedroom', 'kitchen' ] } 

相反,它正在通过

 { product_name: 'light', product_price: '100', description: 'its a light', categories:'kitchen' } 

先谢谢你。

你的问题是你在multipart/form-data表单上使用bodyParser。 bodyParser的文档说:“这不处理多部分机构,由于其复杂,通常大的性质。

如果您从表单中删除multipart/form-data ,并且不尝试上传文件,则会看到bodyParser能够为您提供所需的格式,但是您将无法上传文件。

所以你必须使用另一个处理multipart/form-data bodyParser。 另一种select是垂涎 。 你只需要对你的代码做两个修改就可以使用multer:

 var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var MongoClient = require('mongodb').MongoClient; var db; // 1. require multer and define where you want the file to be uploaded var multer = require('multer'); var upload = multer({ dest: 'uploads/' }); app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); MongoClient.connect('xxxxxxxxxxxxxx', function (err, database) { if (err) return console.log(err); db = database; app.listen(3000, function () { console.log('listening on 3000'); }); }); // 2. Add middleware here with the name of the file input app.post('/addProduct', upload.single('sampleFile'), function (req, res) { db.collection('products').save(req.body, function (err, result) { if (err) return console.log(err); console.log('saved to database'); res.redirect('/'); }); }); 

parsing器body-parser使用qs应该将具有多个值的参数转换为一个数组,但是如果只提供了一个值(即只选中一个checkbox),则qs只返回一个string。 您可以强制它通过在参数名称之后包含[]来始终返回一个数组。

因此,请尝试将表单中的所有name="categories"更改为name="categories[]"


下面是一个例子,显示了qs如何parsing表单数据:

 const qs = require('qs') // qs just returns a as a string. console.log(qs.parse('a=1')) // The [] make qs always return an array. console.log(qs.parse('a[]=1')) console.log(qs.parse('a[]=1&a[]=2')) 

https://runkit.com/5818badec3b80300148b2c65/5818bbf9c3b80300148b2d24