1、Schema hasn't been registered for model "User":在对两张表Article和User进行关联后调用author时报错
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true, useUnifiedTopology: true }).then(() => {
console.log('数据库连接成功');
}).catch(err => {
console.log('数据库连接失败');
});
const articleTable = mongoose.model('Article', new mongoose.Schema({ title: String, author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } }));
articleTable.find().populate('author').then(res => {
console.log(res);
})
解决办法:在查询前获取User表
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true, useUnifiedTopology: true }).then(() => {
console.log('数据库连接成功');
}).cat