Array property in a Javascript object undefined

I use Node.js to process an array from the results obtained from Mongoose: Artist

User.find({UserType: "Artist"}, '_id Firstname BIO list_artworks').then((artists) =>{
    for (var i=0; i < artists.length; i++){
        console.log(artists[i]);
    }
})

In the above code, searching and printing all three properties that I requested works . Artists

EDIT: Here is an example of what I see in the console:

{
    _id: 'T8fdSmf0e1ua',
    BIO: 'This is Susan bio...\n',
    Firstname: 'Susan',
    list_artworks: [
        'ID ONE',
        'ID TWO', 
        ...
    ]
}

However, when I try to access a property containing an array , all undefined use : Artwork console.log()

for (var i=0; i < artists.length; i++){
    console.log(artists[i].list_artworks);
}

When I access other properties of my Artist, for example, or , it successfully prints on my console: BIO Firstname

for (var i=0; i < artists.length; i++){
    console.log(artists[i].BIO);
}

? , , , then(). list_artworks , Artist, undefined ?

. , :

console.log(artist.BIO);
console.log("---------------")
console.log(artist.Firstname);
console.log("---------------")
console.log(artist.list_artworks);
console.log("---------------")
console.log(artist)

:

 
---------------
Mike
---------------
undefined // <--- when I access the property itself
---------------
{
  _id: '599asdsdasd232d23d2',
  Firstname: 'Mike',
  BIO: '',
  list_artworks: // <-- why does it show up here?
   [ '6cohpx7d23asu',
     'W4Fgs23X5aXds',...

:

setTimeout(function(){

     User.find({UserType: "Artist"}, '_id Firstname BIO list_artworks').then((artists) =>{
         console.log(artists.length);

        for (var i=0; i < artists.length; i++){
            artist = artists[i];
             console.log(artist.BIO);
             console.log("---------------")
             console.log(artist.Firstname);
             console.log("---------------")
            console.log(artist['list_artworks']);
             console.log("---------------")
             console.log(artist)
            }
        }

     ).catch((err)=>{console.log(err)});
}, constants.NLP_TRAINING_INTERVAL);
+4
3

toObject :

artist = artists[i].toObject();
+2

User.find({UserType: "Artist"}, '_id Firstname BIO list_artworks').then((artists) =>{

        for (var i=0; i < artists.length; i++){
            console.log(artists[i][‘list_artworks’]);
        }
         }
0

Try the following:

User.find({UserType: "Artist"}, '_id Firstname BIOlist_artworks').then((artists) =>{
             var index = i.toString();
            console.log(artists[index][‘list_artworks’]);
        }
   }
0
source

Source: https://habr.com/ru/post/1681025/


All Articles