node.js - 使用 Mongoose .find() 在 EJS 模板中渲染数据
问题描述
我正在创建一个简单的 CRUD 应用程序。问题是:它连接到MongoDB就好了,我可以正常保存数据,也可以删除它。但是我好像找不到!我不知道错误是在 EJS 中还是在后端。
这是错误:
这是后端代码:
//search lessons
exports.searchLesson = (req, res) => {
let searchField = req.body.searchField;
if (req.query.id) {
const id = req.query.id;
Lesson.findById(id)
.then(data => {
if (!data) {
res.status(404).send({
message: 'User not found'
})
} else {
res.send(data)
}
})
.catch(err => {
res.status(500).send({
message: 'Error retrieving lesson with id ' + id
})
});
} else {
Lesson.find({
teacher: searchField
})
.then(lesson => {
res.render('searchLesson.ejs', {
lessons: lesson
})
})
.catch(err => {
res.status(500).send({
message: err.message || "Error occured while retrieving lesson information"
});
});
}
};
怎么了?
解决方案
Gabriel Moura 一旦尝试lesson
而不是lessons
在前端这是类型错误,这意味着我们拼错了一些我们必须在控制器和 ejs 文件中更正的内容......
推荐阅读
- docker - docker-compose 如何将我的服务名称映射到 IP 地址?
- javascript - 如何将序列化的多维数组从 C# 传递到 Javascript
- blazor - 将导航参数传递给 blazor 中的导航或布局组件
- python - 如何避免列表理解中重复元素的重复计算索引
- xamarin - 为什么在使用 Xamarin Android 应用程序中的 APK 时 GStreamer 播放会受到影响?
- nestjs - 是否可以创建一个也是请求范围的自定义提供程序?
- python - 为什么我不能正确分配给 3d 列表?
- javascript - CKEditor 5 (ckeditor-angular) 的通用文件上传器
- swift - 使用 Swift 读取/写入 macOS 的 DNS 设置?
- python - 赋值前引用的 Jupyter Notebook 错误局部变量