2017-06-11 2 views
0

私はユーザーが自分のuserIdで作成したブログをすべて見つけようとしています。 私は、ブログ私のserver.jsで、その後マングース「find」は空の配列を返します

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

私はこの

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


}) 

持っていますが、それは空の配列を返すために、次のマングースモデルを持っている私はこれをどのようにアプローチすべきか?

+0

を試してみてください。もちろん、あなたはマッチを得ることはありません。 'Blog.find({_ creator:id})'などのようなものでなければなりません。 –

+0

だから私はそれが作成者のIDとして、ユーザーIDを扱うようにする必要がありますか? – Stan

答えて

1

ライン

return Blog.find(id).then((blog) => { 

はおそらく

return Blog.find({ _creator: id }).then((blogs) => { 
+0

あなたは正しいです:) – Stan

+0

あなたは男です:) – Stan

+0

申し訳ありません私は最初から質問を完全には読んでいませんでした – maxpaj

0

を言うべきあなたの `findBlogs`は、ユーザーIDを取り、その後、ブログIDとして扱い、この

blog.find({creatorid:user.id},function(err,data){ 
//data will be here 
}) 
関連する問題