2017-06-11 9 views
0

Ich versuche alle Blogs zu finden, die ein Benutzer mit seiner userId erstellt hat. Ich habe folgendes Mungo Modell für das BlogMongoose "find" gibt ein leeres Array zurück

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

dann in meinem server.js ich habe diese

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


}) 

aber es gibt ein leeres Array, wie soll ich diesen Ansatz?

+0

Ihre 'findBlogs' nimmt eine Benutzer-ID und dann behandelt es als ein Blog-ID sagen sollte. Natürlich bekommst du keine Übereinstimmungen. Sollte 'Blog.find ({_ creator: id})' oder so ähnlich sein. –

+0

Also was muss ich tun, damit es die Benutzer-ID behandelt, wie die Ersteller-ID? – Stan

Antwort

1

Die Linie

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

wahrscheinlich

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

Sie haben Recht :) – Stan

+0

Sie sind der Mann :) – Stan

+0

Sorry, ich habe die Frage nicht vollständig von Anfang an gelesen – maxpaj

0

Versuchen Sie, diese

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