Sunday, 11 June 2017

Mongoose "find" return an empty array

i'm trying to find all blogs that a user has created with his userId. I have the following mongoose model for the blog

 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};

then in my server.js i have this

 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)
  })


 })

but it returns an empty array, how should i approach this?



via Stan

No comments:

Post a Comment