Javascript Node.js:mongoose.populate()未显示数据
我试图显示数据库中的数据。我有3个模式,将它们组合在一起。但是,综合数据没有显示出来。我已经附上了我的3个模式 异步等待在try-catch中运行良好,这对我来说似乎很干净。我也试着跟随。两者都返回相同的结果 需要提到的是:我是个新手。因此,对于要遵循的最佳实践没有好的想法 书籍架构:Javascript Node.js:mongoose.populate()未显示数据,javascript,node.js,express,mongoose,Javascript,Node.js,Express,Mongoose,我试图显示数据库中的数据。我有3个模式,将它们组合在一起。但是,综合数据没有显示出来。我已经附上了我的3个模式 异步等待在try-catch中运行良好,这对我来说似乎很干净。我也试着跟随。两者都返回相同的结果 需要提到的是:我是个新手。因此,对于要遵循的最佳实践没有好的想法 书籍架构: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const BookSchema = new Schema({
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BookSchema = new Schema({
title: {
type : String,
required : [true, 'Book Title is Required'],
max : 100,
min : 5,
trim : true,
lowercase: true
},
author: {
type : Schema.Types.ObjectId,
ref : 'Author',
required: [true, 'Author is Required']
}
genre: [{
type: Schema.Types.ObjectId,
ref : 'Genre'
}]
}, { collection : 'book', timestamps: true });
BookSchema
.virtual('url')
.get(() => {
return 'book/' + this._id;
});
module.exports = mongoose.model('Book', BookSchema);
作者模式:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const AuthorSchema = new Schema({
firstName: {
type : String,
required : [true, 'First Name is Required'],
max : 100,
min : 5,
trim : true,
lowercase: true
},
lastName: {
type : String,
required : [true, 'Last Name is Required'],
max : 100,
min : 5,
trim : true,
lowercase: true
}
}, { collection : 'author', timestamps: true });
AuthorSchema
.virtual('name')
.get(() => {
return this.firstName + this.lastName;
});
module.exports = mongoose.model('Author', AuthorSchema);
类型模式:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const GenreSchema = new Schema({
name: {
type : String,
required : [true, 'Genre Name is Required'],
max : 100,
min : 3,
trim : true,
lowercase: true
}
}, { collection : 'genre', timestamps: true });
module.exports = mongoose.model('Genre', GenreSchema);
图书管理员:
exports.bookList = async(req, res, next) => {
try {
const bookList = await Book.find({}).populate('author').exec();
res.render('./book/index', { title: 'Book List', bookList: bookList});
} catch (error) {
res.status(500).json({ message: error.message });
}
};
index.pug:
ul
each book in bookList
li
a(href=book.url) #{book.title}
| (#{book.author.name})
else
li No book Has Been Listed Yet...!
(NaN)请尝试以下代码。我想,它会起作用的
exports.bookList = async(req, res, next) => {
try {
const bookList = await Book.find({}).populate('author').exec((error, list) => list);
res.render('./book/index', { title: 'Book List', bookList: bookList});
} catch (error) {
res.status(500).json({ message: error.message });
}
};
请尝试以下代码。我想,它会起作用的
exports.bookList = async(req, res, next) => {
try {
const bookList = await Book.find({}).populate('author').exec((error, list) => list);
res.render('./book/index', { title: 'Book List', bookList: bookList});
} catch (error) {
res.status(500).json({ message: error.message });
}
};
在我的查询中,我只需要添加如下回调:
exports.bookList = async(req, res, next) => {
try {
const bookList = await Book.find({}).populate('author').exec((err, bookList) => {
if (err) return bookInstanceList;
// console.log(bookList);
res.render('./book/index', { title: 'Book List', bookList: bookList});
});
} catch (error) {
res.status(500).json({ message: error.message });
}
};
主要问题是模式中的箭头函数。我使用了箭头函数来获取对象。但是,箭头函数不适用于对象。以下是参考资料:在我的查询中,我只需要添加如下回调:
exports.bookList = async(req, res, next) => {
try {
const bookList = await Book.find({}).populate('author').exec((err, bookList) => {
if (err) return bookInstanceList;
// console.log(bookList);
res.render('./book/index', { title: 'Book List', bookList: bookList});
});
} catch (error) {
res.status(500).json({ message: error.message });
}
};
主要问题是模式中的箭头函数。我使用了箭头函数来获取对象。但是,箭头函数不适用于对象。以下是引用:显示以下错误:无法读取的属性“length”undefined@tanjiya在返回结果之前,请将错误记录在exec函数中。像这样:exec((error,list)=>{console.log(error);return list;})并让我知道返回500和null的错误(如果有),那么,这个错误是否为null?如果是这样,您的查询执行得很好。在res.render中可能有一些错误。500表示内部服务器错误。它与查询不关联。另外,请将list参数记录到console.log中,并查看其中的内容。console.log(bookList)正在控制台中返回数据。显示以下错误:无法读取的属性“length”undefined@tanjiya在返回结果之前,请将错误记录在exec函数中。像这样:exec((error,list)=>{console.log(error);return list;})并让我知道返回500和null的错误(如果有),那么,这个错误是否为null?如果是这样,您的查询执行得很好。在res.render中可能有一些错误。500表示内部服务器错误。它与查询不关联。另外,请将list参数记录到console.log中,并查看其中的内容。console.log(bookList)正在控制台中返回数据。