Tips for consolidating outputs from three different APIs using JavaScript and AJAX? [Pseudo code example]

For my school project, I am working on an e-commerce aggregator site where I need to combine product data from 3 different APIs (like Aliexpress and Amazon) into one homepage. Although I can retrieve results from each API individually, I'm facing challenges in storing these results in a collated_results_list. Whenever I try to append or return the individual_results to the collated_results_list, it either shows undefined or doesn't add the individual_results at all.

The project is mainly focused on Javascript and AJAX, but I can also incorporate other libraries as long as they don't replicate the functionalities of JS and AJAX. Any assistance in resolving this issue would be highly appreciated. Thank you!

The following pseudocode represents the general structure of my script:

//Inside populate_search_result_page.js

function collate_results(){

    var collated_results_list = [];

    var amazon_list = call_amazon_api(); // returns undefined
    var aliexpress_list = call_aliexpress_api(); // returns undefined
    var taobao_list = call_taobao_api(); // returns taobao_list = undefined;
    
    //collate all results from all APIs
    collated_results_list.concat(amazon_list, aliexpress_list, taobao_list ); 
    
    for loop (collated_results_list) {
        Using collated_results_list, populate search result page in a sorted manner;
    } 
}

function call_amazon_api(){
    return amazon_product_list; //results can be printed within call_amazon_api, but cannot be passed to another global variable
}

function call_aliexpress_api(){
    return aliexpress_product_list;
}

function call_taobao_api(){
    return taobao_product_list;
}

Answer №1

It appears that there may be an issue in the code you provided. I recommend troubleshooting the specific section mentioned below.

function retrieve_data_from_api(){
    console.log("api_data" + api_data); //debugging this line could help identify the issue
    return api_data; //data can be accessed within this function, but not passed to a global variable
}

function process_external_api_response(){
    console.log("external_api_response "+ external_api_response); //checking this line might reveal the problem
    return external_api_response;
}

function handle_api_errors(){
    console.log("api_error_message " + api_error_message); //investigating this line could resolve the issue
    return api_error_message;
}

It seems likely that this portion of your code is causing complications.

Please inform me if this information proves useful.

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

Creating a JSON API with Express Framework

I'm looking to create a JSON API. One issue I'm facing is the need to query for both an ID and a string at different times. One approach could be to include a querystring in the URL, like so: example.com/user/RandomName example.com/user/123455 ...

Trying out the fetch api with Jest in a React Component: A step-by-step guide

As a newcomer to test driven development, I stumbled upon a section that talked about testing/mocking a fetch API. However, I am facing issues while trying to write my own test. In order to practice this concept, I created a simple weather app where I atte ...

Ensuring User Input Integrity with JavaScript Prompt Validation

I need help with validating input from a Javascript prompt() in an external js file using HTML code. I know how to call the Javascript function and write the validation logic, but I'm unsure how to handle the prompt and user input in HTML. Do I need ...

Ways to share code across AngularJS frontend and Node.js backend

How can code be effectively shared between an AngularJS client and a Node.js server? After creating an AngularJS application, I now need to develop a RESTful server to provide data to the client. Some Angular services used on the client-side could also be ...

Create a modal using the base of a[href^=“#id”]

What is the best way to minimize a call modal window? $('#button-1').on('click', function() { $('#modal-1').addClass('j-modal--open'); }); $('#button-2').on('click', function() { $(' ...

Troubleshoot: JQuery unable to change background color in code

Is there a way to use JQuery to cycle through an array of colors and change the background color of a page whenever a specific button is clicked? I've tried implementing it with the following code, but it doesn't seem to work as expected. Here&a ...

Incorporating Axios data into a React component

I am seeking assistance with React development as I am facing challenges in mapping the data retrieved from a simple API call using Axios. This problem has been troubling me for a few days now. Below, you will find my App.js App Component along with the AP ...

Having trouble creating a PDF from HTML

Having trouble with generating PDFs using various libraries as I keep encountering the following error: Fatal Error: spawn UNKNOWN The code snippet looks like this: mammoth.convertToHtml({ path: './backend/common/template.docx' } ...

What is the process for including parameters in a javascript/jquery function?

This unique script enables you to click on a specific element without any consequences, but as soon as you click anywhere else, it triggers a fade-out effect on something else. Within the following code snippet, you can interact with elements inside $(&apo ...

Leaflet OSM: Adjust map focus to the polygon's center

Looking to create an HTML file that integrates the Leaflet library to display an OpenStreetMap view with a centered polygon? I've been following a helpful discussion, but still unsure about how to center and autozoom an arbitrary polygon on the map. A ...

Ejs does not automatically include the message when returning a template to render in HTML

I need help rewording the title properly. I am facing an issue in my server code where a database query is executed to check if the account name already exists. If it does, a specific message should be displayed using this template: con.query('SELECT ...

The package.json file is missing the "exports" main, causing the Error [ERR_PACKAGE_PATH_NOT_EXPORTED] to be displayed

Can anyone help me resolve the 'Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in package.json' issue? I've been struggling to fix it despite receiving assistance. The problem is quite baffling and the more I dwel ...

Unit testing Vue 3 by simulating the push() method of vue-router

As a newcomer to Vue and StackOverflow, I wanted to share my issue. While attempting to run the unit test for my application, I encountered a TypeError. The error message stated: "TypeError: Cannot read properties of undefined (reading 'push')" ...

JavaScript rearrange array elements

Currently, I'm attempting to move the values of an array over by a random amount. For instance: var array = [1,2,3,4]; var shiftAmount = 1; The goal is to shift the array so that it looks like [4,1,2,3] ...

"JQuery event handlers not functioning as expected: .click and .on failing

For some time now, I've been facing this issue and I'm at a loss trying to figure it out. I have attempted various solutions such as using .click(), .on(), .delegate, and even utilizing .find() to locate the specific element causing the problem. ...

Creating a dynamic map in AngularJS by parsing and utilizing data from a JSON file

Looking to create a map from JSON data returned by a RESTFUL WEB Service using AngularJS. Here is an example of the JSON format: myJSON = [{ id: 8, color: "red", value: "#f00" }, { id: 37, color: "green", value: "#0f0" }, { id ...

Anticipating the arrival of data from an unspecified child component prior to executing the effect

Suppose there is a parent component: function Parent({ children }) { useEffet(() => { // Perform action only after all children have returned their values via props (or context) }) return <div>{ children }</div> } How do I ensure ...

Looking to locate a specific video within the DOM

Is there a way to detect the presence of video content in the DOM, regardless of how it is inserted? I attempted using phantomJS but was unable to find a satisfactory method. ...

parallax scrolling can be a bit bumpy

While working on a website, I've incorporated a slight parallax effect that is functioning almost perfectly. However, I've noticed that the foreground divs tend to jump a little when scrolling down the page. At the top of the page, there is a di ...

The search function on my blog is not displaying the blogs that have been filtered

I have encountered an issue with my code as I am unable to get any search results from the search bar. const RecentBlogs = ({recentBlogs}) => { const [query, setQuery] = useState("") const filteredItems = (() => { if(!query) return rec ...