javascript - 通过 nodejs 在 mongodb 中编辑对象
问题描述
我正在尝试学习一些 NodeJS。我正在尝试将便笺应用程序编辑为盒子应用程序。最终目的是拥有一个应用程序,我可以在其中定义一些框并进行编辑(编辑它的内容)。从https://github.com/FaztTech/nodejs-notes-app代码开始,我做了一些更改。
编辑架构:
const { Schema, model } = require("mongoose");
const CajaSchema = new Schema(
{
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
contenido: {
type: Object,
required: true
},
user: {
type: String,
required: true
}
},
{
timestamps: true
}
);
module.exports = model("Caja", CajaSchema);
编辑控制器:
const notesCtrl = {};
// Models
const Caja = require("../models/Note");
notesCtrl.renderNoteForm = (req, res) => {
res.render("notes/new-note");
};
notesCtrl.createNewNote = async (req, res) => {
const { title, description, contenido } = req.body;
const errors = [];
if (!title) {
errors.push({ text: "Please Write a Title." });
}
if (!description) {
errors.push({ text: "Please Write a Description" });
}
if (!contenido) {
errors.push({ text: "Please Write a contenido" });
}
if (errors.length > 0) {
res.render("notes/new-note", {
errors,
title,
description,
});
} else {
const newNote = new Caja({ title, description, contenido });
newNote.user = req.user.id;
await newNote.save();
req.flash("success_msg", "Note Added Successfully");
res.redirect("/notes");
}
};
notesCtrl.renderNotes = async (req, res) => {
const notes = await Caja.find({ user: req.user.id })
.sort({ date: "desc" })
.lean();
res.render("notes/all-notes", { notes });
};
notesCtrl.renderEditForm = async (req, res) => {
const note = await Caja.findById(req.params.id).lean();
if (note.user != req.user.id) {
req.flash("error_msg", "Not Authorized");
return res.redirect("/notes");
}
res.render("notes/edit-note", { note });
};
notesCtrl.updateNote = async (req, res) => {
const { title, description, contenido } = req.body;
await Caja.findByIdAndUpdate(req.params.id, { title, description, contenido });
req.flash("success_msg", "Note Updated Successfully");
res.redirect("/notes");
};
notesCtrl.deleteNote = async (req, res) => {
await Caja.findByIdAndDelete(req.params.id);
req.flash("success_msg", "Note Deleted Successfully");
res.redirect("/notes");
};
module.exports = notesCtrl;
编辑-note.hbs:
{{#each errors}}
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{text}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{{/each}}
<div class="col-md-4 mx-auto">
<div class="card">
<div class="card-header">
<h3>Edit Note</h3>
</div>
<div class="card-body">
<form action="/notes/edit-note/{{note._id}}?_method=PUT" method="POST">
<input type="hidden" name="_method" value="PUT">
<div class="form-group">
<input type="text" name="title" class="form-control" value="{{note.title}}" />
</div>
<div class="form-group">
<textarea name="description" class="form-control">{{note.description}}</textarea>
</div>
<div class="form-group">
<div class="input-group flex-nowrap">
{{#each note.contenido}}
<div class="form-group">
<p>{{@key}}</p>
<textarea name="{{@key}}" class="form-control">{{this}}</textarea>
</div>
{{/each}}
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">
Save
</button>
</div>
</form>
</div>
</div>
</div>
我的问题是,当我编辑注释时,mongodb 中的对象“contenido”会更新为 null 而不是新值。怎么了?
解决方案
推荐阅读
- python - 如何使用 PyTorch 0.4.0 从 numpy 数组中使用 requires_grad=True 制作 FloatTensor?
- wordpress - 如何恢复wordpress备份?
- last.fm - 使用 Last.fm api 获取歌曲元数据
- python - 如何预处理音频数据以输入神经网络
- c# - OAuth 2 脚本错误
- php - 使用 PDO PHP 将 CSV 导入 MySQL 数据库
- angular - 从 Angular 5 到 Lumen Rest API 的 HTTP 请求
- javascript - Javascript:resquestAnimationFrame 作为对象属性
- r - R比较数据框中两个因素的值并在第三个中获得结果
- reactjs - 反应原生中的不变错误找不到商店