user2466886 user2466886 - 1 month ago 11
Node.js Question

Updating MongoDB with checkbox information

So I'm learning Node.js and Express and using it to make a simple web app that conducts CRUD operations on a mongoDB database. I can successfully insert information into the database and update it however I cannot seem to figure out how to update checkbox information. Note I looked at Passing checkbox values to database using JavaScript and it didn't help since it's in meteor.js

In my EJS file. This is the information the user has to insert in order to update a matching entry in the database. I've omitted some elements of the form for the sake of brevity.

<div id="editForm" hidden>
Enter name of the entry you want to edit?<input type="text" placeholder="full name" id="editItem" name="editItem" required><br>
Full Name<input type="text" placeholder="full name" id="fullName" name="name" required><br>
Age <input type="number" name="age" min="18" max="95" id="age" required><br>
Programming Languages <br> <input type="checkbox" class="p_languages2" name="p_languages2" value="Java" required>Java
<input type="checkbox" name="p_languages2" class="p_languages2" value="C++">C++
<input type="checkbox" name="p_languages2" class="p_languages2" value="Python">Python
<input type="checkbox" name="p_languages2" class="p_languages2" value="Ruby">Ruby
<input type="checkbox" name="p_languages2" class="Jp_languages2" value="JavaScript">JavaScript<br>
Available to start work? <input type="date" id="start_date" name="start_date" required><br>
Life Motto? <input type="text" placeholder="motto" id="motto" name="motto" required><br>
<button onClick='putRequest()' type="submit" id="editEntry">Submit</button>

function putRequest() {
fetch('/view', {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
'editedItem': document.getElementById("editItem").value,
'name': document.getElementById("fullName").value,
'age': document.getElementById("age").value,
//how can I extract the checkboxes?
'start_date': document.getElementById("start_date").value,
'motto': document.getElementById("motto").value


And here's my put request for extra information. Note that "p_languages": req.body.p_languages is just a placeholder, I know it doesn't work.

app.put('/view', function(req, res) {
console.log("Put is working!");
{"name": req.body.editedItem },
{ "name": , "job_title": req.body.job_title, "gender": req.body.gender,
"p_languages": req.body.p_languages, "age": req.body.age, "start_date": req.body.start_date,"motto": req.body.motto }
function(err, object) {
if (err){
console.warn(err.message); // returns error if no matching object found
console.log("Update complete");



You can refer to all checkboxes by name attribute like this, var checkboxes = document.getElementsByName("p_languages2");. This will return array checkboxes with p_languages2. Now you just need to iterate over this array and get the value of all the checkboxes which are checked checkboxes[i].checked and store that values in an array and update that array on server. Refer to this plnkr.