如何解决将布尔值从mongodb传递到html的问题
我在尝试将布尔值传递给html时遇到问题。我正在使用node.js,mongodb和mongoose。我有一个带有两个复选框的表单。在我的猫鼬模式中,我有一个checkbox1和checkbox2。它们都是布尔类型,默认值为false
现在,在我的表单中,如果用户选中该框,它将触发PUT路由并应更新mongodb。从假到真,反之亦然。我可以将值从false更改为true,但无法将值从true更改为false。在终端中,我收到错误消息“无法读取null的属性'checkbox1'。
这是我的app.js:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
const _ = require("lodash");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine","ejs");
app.use(express.static("public"));
app.use(methodOverride("_method"));
mongoose.connect("mongodb://localhost:27017/budgetDB",{
useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false,});
const itemSchema = {
item: {
type: String,required: true,},dueDate: {
type: String,amount1: {
type: mongoose.Decimal128,amount2: {
type: mongoose.Decimal128,notes: {
type: String,checkbox1: {
type: Boolean,checkbox2: {
type: Boolean,};
const Item = mongoose.model("Item",itemSchema);
app.get("/",function (req,res) {
res.render("index");
});
app.get("/month",res) {
Item.find({},function (err,itemFound) {
res.render("monthTemplate",{ newItems: itemFound });
});
});
app.post("/month",res) {
const itemDesc = req.body.item;
const billDueDate = req.body.billDueDate;
let checkboxStatusA = req.body.checkboxA;
const amountForA = req.body.amountForA;
let checkboxStatusU = req.body.checkboxU;
const amountForU = req.body.amountForU;
const notesForItem = req.body.notes;
if (checkboxStatusA === "on") {
checkboxStatusA = true;
} else {
false;
}
if (checkboxStatusU === "on") {
checkboxStatusU = true;
} else {
false;
}
const newItem = new Item({
item: itemDesc,dueDate: billDueDate,checkbox1: checkboxStatusA,amount1: amountForA,amount2: amountForU,checkbox2: checkboxStatusU,notes: notesForItem,});
newItem.save();
res.redirect("/month");
});
app.get("/items/:itemId",res) {
const requestItemId = req.params.itemId;
Item.findOne({ _id: requestItemId },item) {
res.render("item",{
itemDesc: item.item,billDueDate: item.dueDate,amountForA: item.amount1,checkboxA: item.checkbox1,amountForU: item.amount2,checkboxU: item.checkbox2,billNotes: item.notes,});
});
});
app.put("/checkbox",res) {
const checkboxCheckedID = req.body.checkboxA;
console.log(checkboxCheckedID);
Item.findById(checkboxCheckedID,itemFound) {
if (err) {
console.log("Did not find the item");
} else if (itemFound.checkbox1 === false) {
Item.findByIdAndUpdate(
req.body.checkboxA,{ $set: { checkbox1: true } },{ new: true },itemFound) {
if (err) {
console.log(err);
} else {
console.log("changed successful");
res.redirect("/month");
}
}
);
} else if (itemFound.checkbox1 === true) {
Item.findByIdAndUpdate(
req.body.checkboxA,{ $set: { checkbox1: false } },itemFound) {
res.redirect("/month");
}
);
}
});
// end of PUT route
});
app.listen(3000,function () {
console.log("Server running on port 3000");
});
这是我表格的摘录:
<% newItems.forEach(function(item){ %>
<tr>
<th scope="row"><a href="/items/<%=item._id %>"><%=item.item %></a></th>
<td><%=item.dueDate %></td>
<form action="/checkbox?_method=PUT" method="POST">
<td>
<% const aCheckbox = item.checkbox1 %>
<% if ( aCheckbox === true){ %>
<input type="checkbox" name="checkboxA" checked value="<%= item._id%>" onChange="this.form.submit()">
<% } else if (aCheckbox === false) {%>
<% console.log("checkbox not checked") %>
<input type="checkbox" name="checkboxA" value="<%= item._id%>" onChange="this.form.submit()">
<% } %>
<span>$<%=item.amount1 %></span>
任何帮助将不胜感激!
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。