Answer the question
In order to leave comments, you need to log in
How to change empty Booleans to false in mongodb?
Let's say we have this simple form:
<form action="/add">
<input type="checkbox" name="placed">
</form>
let schema = new Schema({
placed: {
type: Boolean,
default: false,
required: true
}
});
Answer the question
In order to leave comments, you need to log in
I solved the issue by inventing the bicycle:
I forcibly added all unchecked checkboxes to FormData via append with a value of false.
More or less like this:
let data = new FormData(form);
let checkboxes = form.querySelectorAll("input[type='checkbox']");
for (let i = checkboxes.length - 1; i >= 0; i--) {
data.append(checkboxes[i].name, checkboxes[i].checked);
}
Your placed field is required - required: true, so if nothing comes, then there will be no changes. Try to remove required: true, then if nothing comes it will be false
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question