猫鼬填充不是一个函数
Mongoose populate is not a function
我希望 post 的创建者成为用户架构。所以我有 2 个架构
post.js
const mongoose=require('mongoose');
mongoose.Promise = global.Promise;
const Schema= mongoose.Schema;
const postSchema= new Schema({
body:{ type: String, required:true, validate:bodyValidators},
createdBy: { type: Schema.Types.ObjectId,ref:'User'}, // this one
to: {type:String, default:null },
createdAt: { type:Date, default:Date.now()},
likes: { type:Number,default:0},
likedBy: { type:Array},
dislikes: { type:Number, default:0},
dislikedBy: { type:Array},
comments: [
{
comment: { type: String, validate: commentValidators},
commentator: { type: String}
}
]
});
module.exports = mongoose.model('Post',postSchema);
user.js
const mongoose=require('mongoose');
mongoose.Promise = global.Promise;
const Schema= mongoose.Schema;
const userSchema=new Schema({
email: { type: String, required: true, unique: true, lowercase: true, validate: emailValidators},
username: { type: String, required: true, unique: true, lowercase: true, validate: usernameValidators},
password: { type: String, required: true,validate: passwordValidators},
bio: { type:String,default:null},
location: {type:String, default:null},
gender: {type:String,default:null},
birthday: { type:Date,default:null},
img: { type:String, default:'Bloggy/uploads/profile/avatar.jpeg'}
});
module.exports = mongoose.model('User',userSchema);
当用户创建一个新的 post 时,我将他的 _id 保存到一个新的 post 对象中
const post= new Post({
body: req.body.body,
createdBy:user._id,
createdAt:Date.now()
});
当我想恢复所有 post 的指定作者时
router.get('/allPosts',(req,res)=>{
Post.find().populate('createdBy').exec((err,posts)=>{
if(err){
res.json({success:false,message:err});
}
else{
if (!posts) {
res.json({success:false,message:"No posts found"});
}
else{
res.json({success:true,posts:posts});
}
}
}).sort({'_id':-1}); // the latest comes first
});
虽然我关注了 the documentation,但它不起作用。我得到的错误是 TypeError: Post.find(...).populate(...).exec(...).sort is not a function
我究竟做错了什么 ?我错过了什么吗?也许这两个模型不在同一个文件中?
.exec() returns 一个 Promise 并且没有方法调用 .sort()
.
.sort()
在 .exec()
之前,如 Post.find(...).populate(...).sort(...).exec(...)
查看 documentation 中的第三个示例。
删除 execPopulate() 它可能会起作用。它对我有用。
我希望 post 的创建者成为用户架构。所以我有 2 个架构
post.js
const mongoose=require('mongoose');
mongoose.Promise = global.Promise;
const Schema= mongoose.Schema;
const postSchema= new Schema({
body:{ type: String, required:true, validate:bodyValidators},
createdBy: { type: Schema.Types.ObjectId,ref:'User'}, // this one
to: {type:String, default:null },
createdAt: { type:Date, default:Date.now()},
likes: { type:Number,default:0},
likedBy: { type:Array},
dislikes: { type:Number, default:0},
dislikedBy: { type:Array},
comments: [
{
comment: { type: String, validate: commentValidators},
commentator: { type: String}
}
]
});
module.exports = mongoose.model('Post',postSchema);
user.js
const mongoose=require('mongoose');
mongoose.Promise = global.Promise;
const Schema= mongoose.Schema;
const userSchema=new Schema({
email: { type: String, required: true, unique: true, lowercase: true, validate: emailValidators},
username: { type: String, required: true, unique: true, lowercase: true, validate: usernameValidators},
password: { type: String, required: true,validate: passwordValidators},
bio: { type:String,default:null},
location: {type:String, default:null},
gender: {type:String,default:null},
birthday: { type:Date,default:null},
img: { type:String, default:'Bloggy/uploads/profile/avatar.jpeg'}
});
module.exports = mongoose.model('User',userSchema);
当用户创建一个新的 post 时,我将他的 _id 保存到一个新的 post 对象中
const post= new Post({
body: req.body.body,
createdBy:user._id,
createdAt:Date.now()
});
当我想恢复所有 post 的指定作者时
router.get('/allPosts',(req,res)=>{
Post.find().populate('createdBy').exec((err,posts)=>{
if(err){
res.json({success:false,message:err});
}
else{
if (!posts) {
res.json({success:false,message:"No posts found"});
}
else{
res.json({success:true,posts:posts});
}
}
}).sort({'_id':-1}); // the latest comes first
});
虽然我关注了 the documentation,但它不起作用。我得到的错误是 TypeError: Post.find(...).populate(...).exec(...).sort is not a function
我究竟做错了什么 ?我错过了什么吗?也许这两个模型不在同一个文件中?
.exec() returns 一个 Promise 并且没有方法调用 .sort()
.
.sort()
在 .exec()
之前,如 Post.find(...).populate(...).sort(...).exec(...)
查看 documentation 中的第三个示例。
删除 execPopulate() 它可能会起作用。它对我有用。