我正在尝试查找用户使用其 userId 创建的所有博客。 我的博客有以下 Mongoose 模型
var mongoose = require('mongoose');
var BlogSchema = new mongoose.Schema({
title:{
type: String,
required: true,
},
content:{
type: String,
required: true
},
_creator:{
type: mongoose.Schema.Types.ObjectId,
required: true
}
})
BlogSchema.statics.findBlogs = function(id){
var Blog = this;
return Blog.find(id).then((blog)=>{
console.log(blog)
}).catch((e)=>{
console.log('failed')
})
}
var Blog = mongoose.model('Blogs', BlogSchema)
module.exports = {Blog};
然后在我的 server.js 中我有这个
app.get('/get/blogs', authenticate, (req, res)=>{
var userId = req.user._id
console.log(userId)
Blog.findBlogs(userId).then((data)=>{
res.send(data)
}).catch((e)=>{
res.sendStatus(404)
})
})
但它返回一个空数组,我应该如何处理这个问题?
最佳答案
线路
return Blog.find(id).then((blog) => {
应该说
return Blog.find({ _creator: id }).then((blogs) => {
关于javascript - Mongoose "find"返回一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484887/