我正在关注 Odin 的项目本地库教程(Express 应用程序)。 这是我的代码(仅相关部分):
在routes/catalog.js我有这个
router.get("/books", book_controller.book_list);
模型/book.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BookSchema = new Schema({
title:{type:String,required:true},
author:{type:Schema.Types.ObjectId,ref:"Author",required:true},
summary:{type:String,required:true},
isbn:{type:String,required:true},
genre:[{type: Schema.Types.ObjectId,ref:"Genre"}],
});
BookSchema.virtual("url").get(function(){
return `/catalog/book/${this._id}`
})
module.exports = mongoose.model("Book",BookSchema);
控制器/bookController.js
const Book = require("../models/book");
const Author = require("../models/author");
const Genre = require("../models/genre");
const BookInstance = require("../models/bookinstance");
const asyncHandler = require("express-async-handler");
exports.index = asyncHandler(async (req, res, next) => {
// Get details of books, book instances, authors and genre counts (in parallel)
const [
numBooks,
numBookInstances,
numAvailableBookInstances,
numAuthors,
numGenres,
] = await Promise.all([
Book.countDocuments({}).exec(),
BookInstance.countDocuments({}).exec(),
BookInstance.countDocuments({ status: "Available" }).exec(),
Author.countDocuments({}).exec(),
Genre.countDocuments({}).exec(),
]);
res.render("index", {
title: "Local Library Home",
book_count: numBooks,
book_instance_count: numBookInstances,
book_instance_available_count: numAvailableBookInstances,
author_count: numAuthors,
genre_count: numGenres,
});
});
exports.book_list = asyncHandler(async (req, res, next) => {
const allBooks = await Book.find({}, "title author")
.sort({ title: 1 })
.populate("author")
.exec();
res.render("book_list", { title: "Book List", book_list: allBooks });
});
现在这是导致问题的文件:views/book_list.pug
extends layout
block content
h1= title
ul
each book in book_list
li
a(href=book.url) #{book.title}
| (#{book.author.name})
else
li There are no books.
我不知道为什么会导致错误?这显然应该有效,并且在教程中也有效,所以我这里一定有问题,但看在上帝的份上,我找不到错误。 当我转到路线 /catalog/books 时出现错误
D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\views\book_list.pug:10 8| li 9| a(href=book.url) #{book.title} > 10| | (#{book.author.name}) 11| 12| else 13| li There are no books. Cannot read properties of null (reading 'name')
TypeError: D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\views\book_list.pug:10
8| li
9| a(href=book.url) #{book.title}
> 10| | (#{book.author.name})
11|
12| else
13| li There are no books.
Cannot read properties of null (reading 'name')
at eval (eval at wrap (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:120:69)
at eval (eval at wrap (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:155:4)
at template (eval at wrap (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\pug-runtime\wrap.js:6:10), <anonymous>:157:155)
at exports.renderFile (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\pug\lib\index.js:418:38)
at exports.renderFile (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\pug\lib\index.js:408:21)
at exports.__express [as engine] (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\pug\lib\index.js:455:11)
at View.render (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\express\lib\view.js:135:8)
at tryRender (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\express\lib\application.js:640:10)
at Function.render (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (D:\THE ODIN PROJECT\NodeJSCourse\express-locallib-pug\node_modules\express\lib\response.js:1008:7)
我一个人去,只是看看总体思路。当我第一次遇到这个问题时,我认为这是因为我使用的是 EJS,并且教程是由 Pug 进行的。于是我赶紧将文件和视图引擎切换为pug,但根本没有帮助。然后我认为这可能是兼容性问题,所以我通过本地生成项目作为 pug 并复制其余文件来完全克隆这个项目。错误仍然存在——仍然同时存在。 当我删除 book_list.pug 时,没有错误(显然这是由
引起的) > 10| | (#{book.author.name})
line,但是我不知道为什么这会导致我的问题,并且在教程中有效?我也找不到解决方案。
检查您的 项目中的作者模型 是否包含Name字段 或者名称可能为空,请检查 mongodb 检查作者模式中是否存在数据