The error message is: ".map is not recognized as a function in the API

I am facing an issue while trying to map the data from an API result in order to display it. When I run my code, the console displays TypeError: pokemons.map is not a function. Strangely enough, I have implemented a similar functionality in another script and it works perfectly fine there. I'm puzzled as to why .map function is not working this time.

const result = document.getElementById('result');
const form = document.querySelector('form');
let pokemons = [];

async function fetchPokemon() {
  fetch(`https://pokeapi.co/api/v2/pokemon/ditto`)
    .then((res) => res.json())
    .then((data) => (pokemons = data));

  console.log(pokemons)
};

fetchPokemon();

function pokemonDisplay() {

  result.innerHTML = pokemons.map(
    (pokemon) =>
    `
    <h2>${pokemon.forms[0].name}</h2>
    `
  )
}

form.addEventListener('submit', (e) => {
  e.preventDefault();
  fetchPokemon().then(() => pokemonDisplay());
})
console.log(result);
<div class="app">
  <form action="">
    <label for="search">Enter the name of a Pokemon (in English)</label>
    <input type="text" id="search" placeholder="e.g. ditto">
  </form>

  <ul id="result"></ul>
</div>

Answer №1

https://pokeapi.co/api/v2/pokemon/ditto
retrieves an object instead of an array, specifically for the Ditto pokemon. The issue appears to be related to the usage of the map function, which is intended for working with arrays.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to transfer a JavaScript variable through a query string from one HTML page to another?

Is there a way to pass a JavaScript variable called username from one HTML page, example1.html, to another HTML page, example2.html, using query strings? <script type="text/javascript" > $(document).ready(function() { $('#SubmitForm ...

The margin of the parent container is influencing the margin of the child element

Purple Working on displaying a rectangle shape in a browser using divs in my React project. Everything works fine, but when I add margin to the parent base and then draw the boxes, there's some space between the mouse cursor and the actual box. The ...

What is the best approach to accessing a key within a deeply nested array in JavaScript that recursion cannot reach?

After hours of research, I have come across a perplexing issue that seems to have a simple solution. However, despite searching through various forums for help, I have reached an impasse. During my visit to an online React website, I stumbled upon the web ...

Troubleshooting JSON Array Index Problems

I'm having trouble reaching index 3 in the array using the javascript options on my webpage. The final question "are you satisfied with your choice?" is not showing up for me. I'm not sure what I might be missing or doing incorrectly in this sit ...

I am experiencing issues with my Discord.js bot not creating an invite link

Recently, I delved into discord.js but still have much to learn. While working on a bot, I encountered an issue with generating an invite link for the server. Instead of getting the link as expected, an error message popped up: D:\Discord Shield\ ...

Having trouble importing my function component into a router in reactjs. Need some guidance on how to properly set it up

I am working on a Slider component function called export default Slider. In my App.js file, I have the following code: function App() { return ( <Router> <Routes> <Route exact path='/' element={<Home />} /> ...

The overflow-anchor property is not effective for scrolling horizontally

My goal is to create a never-ending horizontal scroll that allows users to scroll both left and right. When users scroll to the left, new content should be added to the beginning of the scrollable element (similar to scrolling through a schedule history). ...

VueJS Vuetify automatically centers default content

Vue cli version @ 5.0.6 | Vuetify version: [email protected] I have been utilizing Vue.js and Vuetify for some time now, and I can't shake the feeling that not all Vue.js/Vuetify components default to centered alignment. I recently initialized a ...

Leveraging Vuex stores in a modular WebPack setup

I am currently working on a web application where each page of the site has 2 JS files: a global "bootstrap.js" file that is consistent across every page and a custom JS file specific to each page. I've run into an issue where I want these files to sh ...

How can I create a continuous color transition effect for the scene background in Three.JS using lerp?

I am currently working on smoothly transitioning between multiple colors for a Day/Night Cycle in the background of my scene. Is it possible to achieve this effect? If so, how can it be done? Additionally, I am curious if it is feasible to incorporate GUI ...

Execute a Jquery function following an automatic focus

Is it feasible to activate a function after an autofocus event? I attempted it, but when my textarea receives autofocus, I have to click away and then click on the textarea again to trigger the function. Here is my code: $('#test').focus(); $(& ...

What is the method for assigning a value to a JSON object using data from another JSON object?

I am faced with the task of setting the seqNo property in one JSON object, b, based on the id from another JSON object, a. How can I achieve this? var a = [{id: "Make", seqNo: 4}, {id: "Model", seqNo: 1}, {id: "XModel", seqNo: 2 ...

The output from `http.send` on the Node + Express backend is displayed as [object object]

When I make a request using http.send in my Node.js backend, it returns [object object]. <script> const newExerciseForm = document.getElementById("newExercise"); newExerciseForm.addEventListener("submit", function (e) { e.pre ...

transferring a JavaScript variable to PHP upon submitting a form

This question arises after my previous inquiry on the topic of counting the rows in an HTML table using PHP. Since I didn't find a solution that worked for me, I am exploring new methods but struggling with the implementation. My approach involves ass ...

Setting up internationalization (i18n) in JavaScript

I am in the process of creating a dynamic webpage using i18n's JavaScript library. I have been following their example code from their homepage located at: However, I am encountering difficulties in loading the specified JSON data, despite adhering t ...

Update a specific line in a file with node.js

What is the most efficient way to replace a line in a large (2MB+) text file using node.js? Currently, I am accomplishing this by Reading the entire file into a buffer. Splitting the buffer into an array by the new line character (\n). Replacing th ...

What is the best way to perform this task in Angular2?

I am currently working with three arrays: tables = [{number:1},{number:2},{number:3},{number:4}]; foods = [ {id:1, name:'Ice Cream'}, {id:2, name:'Pizza'}, {id:1, name:'Hot Dog'}, {id:2, name:'Salad'} ]; o ...

Is the scope causing the inability to update the variable with the final score on the quiz page?

I'm currently developing a quiz page layout. My issue lies with a variable that stores the total score, as I can't seem to display it when the player answers the final question of the quiz. Interestingly, when I log the variable in the console, t ...

The function `canvas.toDataURL()` does not produce an image as output

When I expect the image to return mirrored, it instead shows up as a black image. <!DOCTYPE html> <html> <head> <style> body { margin: 0px; padding: 0px; } </style> </head> <bo ...

The PHP equivalent of converting data to a JSON string, similar to the

When working with PHP's json_encode($array), I've noticed that diacritics can sometimes get messed up. However, if I update my database column to type text and pass javascript-created JSON over HTTP, everything appears fine. The issue arises when ...