复选框数组返回nodejs中的最后一个检查值,而不是整个数组 [英] checkbox array returns the last checked value in nodejs, not whole array
问题描述
我正在尝试通过req.body复选框中的选中值.如果我只检查一个就没有问题,并且在req.body上有一个带有值的对象.如果我检查多个,则返回我检查的最后一个.我正在使用express ejs和express.json()作为正文解析器.
I am trying to take through req.body the checked values from checkbox. If i check only one there is no problem and on req.body there is an object with the value. If i check more than one then it returns the last one in a row i check. I am using express ejs and express.json() as a body parser.
这是我的应用程序文件中的一部分,我在其中配置express.json()
This is a section from my app file where i config the express.json()
const express = require("express");
const path = require("path");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
const hpp = require("hpp");
const cookieParser = require("cookie-parser");
const throwsAnError = require("./utils/throwsAnError");
const checkAuth = require('./middleware/check-auth');
const app = express();
if(process.env.NODE_ENV !== "test"){
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: "Too many request, please try again in an hour"
});
app.use("/api", limiter);
}
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.use(express.static(path.join(__dirname, "public")));
app.use(cookieParser());
app.use(helmet());
// For security reasons we accept body up to 10kb
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: false }));
// Data sanitization (Cleaning the income data from malicious code)
// NoSQL data injection
app.use(mongoSanitize());
// Data sanitization against XSS
app.use(xss());
//Preventing HTTP parameter poloution
app.use(hpp());
路由器是这个:
router.get("/offers/masoutis-offers", authController.isLoggedIn, viewController.masoutisOffers);
router.post("/offers/masoutis-offers", authController.isLoggedIn, viewController.masoutisOffers);
控制器是这样的:
exports.masoutisOffers = async function(req, res) {
console.log(req.body);
const products = await getOffers(req);
res.status(200).render("displayProducts", {
title: "Επιλεγμένες Προσφορές",
products
});
}
async function getOffers(req) {
const page = parseInt(req.query.page || 1);
const sort = req.query.sort || "discountPrice";
const itemsPerPage = 10;
const totalProducts = await Offers.countDocuments();
const products = await Offers.find()
.skip((page - 1) * itemsPerPage)
.limit(itemsPerPage)
.sort(sort);
return {
products,
sort,
currentPage: page,
hasNextPage: itemsPerPage * page < totalProducts,
hasPreviousPage: page > 1,
nextPage: page + 1,
previousPage: page - 1,
lastPage: Math.ceil(totalProducts / itemsPerPage)
}
}
我正在使用的测试表格是这样:
And the test form i am using is this:
<form action="" method="post">
<input type="checkbox" id="hobby" name="stuhobbies" value="sing"/> <strong>Sing</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="guitarplay"/> <strong>GuitarPlay</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="cricket"/> <strong>Cricket</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="football"/> <strong>Football</strong>
<button type="submit">submit</button>
</form>
例如,如果我检查所有值,则作为从console.log(req.body)返回的信息{stuhobbies:'football'}而不是所有值的数组
If for example i check all values, as a return from the console.log(req.body) i receive { stuhobbies: 'football' } instead of an array of all values
推荐答案
What I did and could not take the array was that I had installed a package called hpp https://www.npmjs.com/package/hpp. It removes any array from req.body and puts only the last value from the array. It has an option for whitelisting and this is the way i solved it.
这篇关于复选框数组返回nodejs中的最后一个检查值,而不是整个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!