使用 Mongo 数据库中的值预填充 Mustache.JS 模板 (Mongoose)
Prefill Mustache.JS Template with Values from Mongo DB (Mongoose)
我目前在我的数据库中有一个包含一些条目的列表。现在我希望用户能够编辑其中一个条目。
单击 "Edit" 按钮时,它应该加载原始表单并使用已存储在数据库中的值预填充所有字段。
我在表单中使用了小胡子模板,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Issue Form</h1>
<p>{{ errors }}</p>
<form method="post">
<label>Title: <br><input type="text" name="title">{{ value }}</label><br>
<label>Description: <br><textarea type="text" name="description">{{ value }}
</textarea></label><br>
<label>Priority: <br>
<select name="priority">
<option>high</option>
<option>medium</option>
<option>low</option>
</select>
</label><br>
<input type="submit" value="Save Entry">
</form>
</body>
</html>
这是我的猫鼬模式:
var issueSchema = new mongoose.Schema({
title: String,
description: String,
priority: String
});
我当然对如何填补我的领域做了很多研究。我阅读了 mongoose "populate()" 函数,但是当我尝试使用它时,总是有一些错误告诉我该函数本身是未定义的。
另一种选择是使用 JSON 文件来存储数据值,但在本例中我不能这样做,因为这些值应始终存储在我的 MongoDB 数据文件夹中。
我发现的另一个版本是通过 toObject() 函数创建对象。但无论何时我都尝试过:
router.get('/edit/:id', (req, res) => {
var objectForEditing = issueModel.findOne(req.params.id).toObject;
console.log(objectForEditing);
res.render('issueFormEdit');
});
console.log 部分显示对象未定义。
像我之前在任何其他 javascript 文件中所做的那样使用 JQuery 也不起作用,即使包含模块也是如此。
我只需要一种方法将我的 javascript 代码与我的 hjs 文件连接起来。 但是我根本做不到,我的知识还不够这个。到目前为止,我真的尝试了很多并且投入了数小时。但我根本无法深入了解如何连接这两个文件。
这是我第一次使用 Mustache.JS 和 MongoDB/Mongoose/Express 的这种组合。请温柔一点:(
如果需要更多代码,请告诉我。
您的代码存在以下问题列表:
1) Model.prototype.findOne()
方法是异步的,因此您需要在调用 toObject()
之前使用 async/await
或使用 promises。
2) 您查询猫鼬的方式有误。您需要使用 findOneById(id)
或 findOne({ _id: id })
.
3) toObject
是一个函数,所以必须要调用。
4) objectForEditing
需要作为第二个参数传递给 res.render
函数,它表示 locals,基本上是:
an object whose properties define local variables for the view
试试这个代码 (async/await):
router.get('/edit/:id', async (req, res) => {
let objectForEditing = await issueModel.findOneById(req.params.id);
objectForEditing = objectForEditing.toObject();
res.render('issueFormEdit', objectForEditing);
});
使用承诺:
router.get('/edit/:id', (req, res) => {
issueModel.findOneById(req.params.id)
.then(issue => {
const objectForEditing = issue.toObject();
res.render('issueFormEdit', objectForEditing);
});
});
我目前在我的数据库中有一个包含一些条目的列表。现在我希望用户能够编辑其中一个条目。
单击 "Edit" 按钮时,它应该加载原始表单并使用已存储在数据库中的值预填充所有字段。
我在表单中使用了小胡子模板,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Issue Form</h1>
<p>{{ errors }}</p>
<form method="post">
<label>Title: <br><input type="text" name="title">{{ value }}</label><br>
<label>Description: <br><textarea type="text" name="description">{{ value }}
</textarea></label><br>
<label>Priority: <br>
<select name="priority">
<option>high</option>
<option>medium</option>
<option>low</option>
</select>
</label><br>
<input type="submit" value="Save Entry">
</form>
</body>
</html>
这是我的猫鼬模式:
var issueSchema = new mongoose.Schema({
title: String,
description: String,
priority: String
});
我当然对如何填补我的领域做了很多研究。我阅读了 mongoose "populate()" 函数,但是当我尝试使用它时,总是有一些错误告诉我该函数本身是未定义的。 另一种选择是使用 JSON 文件来存储数据值,但在本例中我不能这样做,因为这些值应始终存储在我的 MongoDB 数据文件夹中。
我发现的另一个版本是通过 toObject() 函数创建对象。但无论何时我都尝试过:
router.get('/edit/:id', (req, res) => {
var objectForEditing = issueModel.findOne(req.params.id).toObject;
console.log(objectForEditing);
res.render('issueFormEdit');
});
console.log 部分显示对象未定义。
像我之前在任何其他 javascript 文件中所做的那样使用 JQuery 也不起作用,即使包含模块也是如此。
我只需要一种方法将我的 javascript 代码与我的 hjs 文件连接起来。 但是我根本做不到,我的知识还不够这个。到目前为止,我真的尝试了很多并且投入了数小时。但我根本无法深入了解如何连接这两个文件。
这是我第一次使用 Mustache.JS 和 MongoDB/Mongoose/Express 的这种组合。请温柔一点:(
如果需要更多代码,请告诉我。
您的代码存在以下问题列表:
1) Model.prototype.findOne()
方法是异步的,因此您需要在调用 toObject()
之前使用 async/await
或使用 promises。
2) 您查询猫鼬的方式有误。您需要使用 findOneById(id)
或 findOne({ _id: id })
.
3) toObject
是一个函数,所以必须要调用。
4) objectForEditing
需要作为第二个参数传递给 res.render
函数,它表示 locals,基本上是:
an object whose properties define local variables for the view
试试这个代码 (async/await):
router.get('/edit/:id', async (req, res) => {
let objectForEditing = await issueModel.findOneById(req.params.id);
objectForEditing = objectForEditing.toObject();
res.render('issueFormEdit', objectForEditing);
});
使用承诺:
router.get('/edit/:id', (req, res) => {
issueModel.findOneById(req.params.id)
.then(issue => {
const objectForEditing = issue.toObject();
res.render('issueFormEdit', objectForEditing);
});
});