Click here to Skip to main content
15,941,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently facing an issue while integrating the OpenWeatherMap API into my web application. I have carefully implemented the code to fetch weather data based on user input, but I am encountering the following error in the console:

Uncaught (in promise) TypeError: locations is not iterable searchTimeout app.js:55 fetchData api.js:14

I have provided the relevant code snippet below for your reference:

"use strict";

import { fetchData, url } from "./api.js";
import * as module from "./module.js";

// Code for toggleSearch, addEventOnElements, and other functions is here...

const searchField = document.querySelector("[data-search-field]");
const searchResult = document.querySelector("[data-search-result]");
let searchTimeout = null;
const searchTimeoutDuration = 500;

searchField.addEventListener("input", function () {
  searchTimeout ?? clearTimeout(searchTimeout);

  if (!searchField.value) {
    searchResult.innerHTML = "";
  } else {

  if (searchField.value) {
    searchTimeout = setTimeout(() => {
      fetchData(url.geo(searchField.value), function (locations) {
        // Code to handle fetched data and display results is here...
    }, searchTimeoutDuration);

 * Fetch Data from Server
 *@param {String} URL API url
 *@param {Function} callback callback
export const fetchData = function (URL, callback) {
    .then((res) => res.json())
    .then((data) => callback(data));

export const url = {
  // API URL construction methods...

I suspect that the issue might be related to the format of the data returned from the API call or how it is being handled within the `fetchData` function. However, I have verified the API key and ensured that the `fetchData` function works as expected for other API endpoints in the application.

I kindly request your assistance in resolving this issue. I would greatly appreciate any insights or suggestions you may have to identify and fix the root cause of this error. Additionally, if there are any improvements I can make to the code structure or API integration approach, please feel free to share your expertise.

If you require any further information or additional code snippets, please let me know, and I will be more than happy to provide them.

What I have tried:

I have already verified the following:

The API call is successful, and the data is being fetched correctly.
The fetchData function returns data in the expected format (an array of objects) for the locations parameter.
The loop to iterate over locations using for...of is implemented correctly.
I suspect the issue might be related to the data received from the API or some asynchronous behavior. I have also tried adding debugging statements to log the type and content of locations to the console, but it didn't provide any specific insights.

Here is the relevant code snippet:

// Inside the searchField event listener
searchTimeout = setTimeout(() => {
  fetchData(url.geo(searchField.value), function (locations) {
    // The loop that causes the error
    for (const { name, lat, lon, country, state } of locations) {
      // Code to process and display the search results...
}, searchTimeoutDuration);

I would appreciate any suggestions or insights on what might be causing this error and how I can resolve it. Thank you in advance for your help!
Andre Oosthuizen 28-Jul-23 10:12am    
a Long shot in the dark, should the 'lon' in your 'name, lat, lon' not be 'long' as in latitude, longitude?
Richard Deeming 1-Aug-23 4:27am    
You need to examine the data which is returned by your url.geo(searchField.value) endpoint. We don't have access to your server, so we can't do that for you. All we know is that it's returning something which is not an array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900