私はユーザーが自分の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)
})
})
持っていますが、それは空の配列を返すために、次のマングースモデルを持っている私はこれをどのようにアプローチすべきか?
を試してみてください。もちろん、あなたはマッチを得ることはありません。 'Blog.find({_ creator:id})'などのようなものでなければなりません。 –
だから私はそれが作成者のIDとして、ユーザーIDを扱うようにする必要がありますか? – Stan