I\'m using Node+Express+Jade to render some webpages. On a form there are 2 checkboxes. When the form is submitted through POST, if the checkbox is checked, I get req.
I solved this problem. Firstly I just want to say that I use jade/pug template engine (+bootstrap 4.1) with ExpressJS on NodeJS.
div.form-group.form-check
if customer.status
input#checkbox.form-check-input(type="checkbox" name="status" value="true" checked)
else
input#checkbox.form-check-input(type="checkbox" name="status" value="false")
NodeJS - customer.js
// Update: Update a customer that given id
router.post('/update/:id', (req, res, next) => {
const customerId = req.params.id;
// let { name, email, city, country, status } = req.body;
// Convert value to Boolean
req.body.status = Boolean(req.body.status);
console.log(req.body); // true | false
Customer.updateOne({ _id: customerId }, req.body).then( /* ... */)
}
html, jade or pug file
NodeJS Javascript file
app.post('/update/:id', (req, res, next) => {
// Firstly, we can try it
console.log(req.body.status); // Output: undefined
// Then we should convert this value to Boolean
req.body.status = Boolean(req.body.status);
console.log(req.body.status) // Output: false or true
}
I hope my solutions help you