1

I'm struggling to understand why my loop is displaying this:

[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]

I tried many things with fetch and axios but it ends the same way 😞

Here is my Svelte component:

<script>
  import axios from 'axios';

  let results = [];

 const fetchYoutubeList = async () => {
    const searchQuery = 'mySearch';
    const maxResults = 10;
    const apiKey = import.meta.env.VITE_APP_YOUTUBE_API_KEY;
    const apiBaseUrl = import.meta.env.VITE_APP_API_BASE_URL;

    await axios.get(`${apiBaseUrl}`, {
      params: {
        part: 'snippet',
        maxResults: maxResults,
        q: searchQuery,
        type: 'video',
        key: apiKey
      }
    })
      .then(response => {
        console.log(response.data);
        results = response.data.items;
        console.log(results);
        console.log(typeof results);
      });
  };
</script>

<section>
  <button
    class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-2"
    on:click|once={fetchYoutubeList}
  >
    Bring me joy
  </button>

  <div>
    <ul>
      {#each results as item }
        <li>
          {item.id}
        </li>
      {/each}
    </ul>
  </div>
</section>

Here the results of my logs: console logs

I think I missed something but I can't find what, so if some of you have an idea I would be glad if you can debug me 😊

Thanks 🙏

5
  • Have you logged the results array to look at the shape of your data? without seeing an example of your results there's not much to say except item.id seems to refer to an object. Commented Feb 4, 2022 at 15:27
  • response.data is a json, then my results is an array but when I log the type is logging object Commented Feb 4, 2022 at 15:30
  • Yes I see that, but arrays are objects (typeof [] === 'object') and can also hold objects. Try logging a single item (console.log(results[0]);) Commented Feb 4, 2022 at 15:31
  • Oh ! I found my mistake here (it's Friday evening 😅 )... My item.id is an object hence the display (weird api returning an object in id field ^^) Thanks, you make me find my mistake 🐛 Commented Feb 4, 2022 at 15:37
  • I had a feeling. ;) Commented Feb 4, 2022 at 15:38

1 Answer 1

1

[UPDATE]- Solution

Thanks to @pilchard, I found my mistake here.

I was trying to loop through an object because my item.id was an object (weird by the way ^^), so everything is ok I manage to do my loop normally. 🔁

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.