繁体   English   中英

猫鼬“查找”返回一个空数组

[英]Mongoose “find” returns an empty array

我正在尝试查找用户使用其userId创建的所有博客。 我的博客有以下猫鼬模型

 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) => {

尝试这个

blog.find({creatorid:user.id},function(err,data){
//data will be here
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM