Reputation:
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?
Upvotes: 0
Views: 1938
Reputation: 1212
Try this
blog.find({creatorid:user.id},function(err,data){
//data will be here
})
Upvotes: 0
Reputation: 6791
The line
return Blog.find(id).then((blog) => {
should probably say
return Blog.find({ _creator: id }).then((blogs) => {
Upvotes: 1