Mongoose find() returns undefined property and strange object

后端 未结 3 1811
庸人自扰
庸人自扰 2021-01-27 16:52

I have a bug that i can\'t resolve because for the first time it happen to me.

here is my query :

    Pack.find(
            {idclient: clientId }
               


        
相关标签:
3条回答
  • 2021-01-27 17:45

    Two parts to this one ...

    First, the callback from a Mongoose find returns an array ... findOne will return a single object.

    As far as the new lines go, mongoose documents have a toString() helper for console.log. It is likely adding the newlines for readability. Wrap the output in JSON.stringify (ie. console.log(JSON.stringify(pack))) prior to calling console.log and you will see the document as a string without the newlines. -http://mongoosejs.com/docs/api.html#document_Document-toString

    0 讨论(0)
  • 2021-01-27 17:54

    find() returns an array, so use findOne(), thanks to Adam Wysocki.

    Sometimes i'm stupid developper .

    0 讨论(0)
  • 2021-01-27 17:57

    As Told Model.find() generates an array so here is how I approach the situation:

    Kitten.find(function (err, kittens) {
        if (err) return console.error(err);
        kittens.forEach(function(kitten){
           console.log(kitten.name);
        }); 
    });
    

    This seems to me the most clear way to access the properties

    0 讨论(0)
提交回复
热议问题