6

I'm using Node.js to loop through an array of Artist results I obtain from Mongoose:

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

The above code works find and prints out all three properties of Artists that I asked for.

EDIT: Here's an example of what I see in console:

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

However, when I try to access a property that is an array of Artwork ids, everything is undefined using console.log():

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

When I access the other properties of my Artist, like BIO or Firstname, it prints successfully to my console:

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

Why is this the case? I don't think it has to do with async code in this case given that all objects are return in the then() chain. The list_artworks property is clearly there if I print the entire artist object, but why is it undefined when I attempt to access it?

Here's another example. I print out each property, and then the object itself:

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

And here's what is printed on my console:

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

Here is my entire code base:

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);
13
  • Are you sure that all of the objects you're getting from MongoDB have a list_artworks key on them? Commented Jul 8, 2017 at 17:08
  • @SeanParsons Yes. I edited my question to include my console.log. There's only three Artists objects in my sandbox DB and they all have that key. Commented Jul 8, 2017 at 17:13
  • Strange that the output starts with ----------, as that is not the first thing you print... Commented Jul 8, 2017 at 17:33
  • @trincot that's because Mike's BIO is empty so it actually did print that line. Just when StackOverflow formatted my output, it cut that text off since it was empty. Commented Jul 8, 2017 at 17:34
  • Is there any other code that could have added the list_artwork property later? Commented Jul 8, 2017 at 17:35

3 Answers 3

5

Call toObject on the artist:

artist = artists[i].toObject();
Sign up to request clarification or add additional context in comments.

2 Comments

This works! Thank you! I took a look at the documentation for toObject() but still am not fully sure why this works- isn't calling artists[i] just returning the ith element (an object) of the array?
artists[i] is a mongoose document, not a regular JS object; calling toObject converts the document into an object.
0

Try this:

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

1 Comment

This returns undefined as well.
-1

Try this

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’]);
        }
         }

1 Comment

If you look at my question, I did exactly this and it returns undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.