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

The jQuery AJAX request is not returning a truthy value when parsing

On my website, I am dealing with a large form and using the serialize() method to process it. However, I have encountered an issue: After completing the form, the result always returns false. I checked this using Firebug. Even though data.ok == true has ...

Incorporating Microsoft's Emotion API into an HTML website

Currently, I am attempting to develop a HTML webpage that can detect emotions from images submitted by the user. By referring to Microsoft's documentation, I have produced the following HTML file: <!DOCTYPE html> <html> <head> & ...

Static response is the way to go! Asynchronous responses just don't cut it

Currently in the process of developing an angular directive for displaying real-time charts. Below is the code snippet that encompasses everything, including link: function() { }, within the directive. Here's the code for a static directive that func ...

Displaying a spinner within Bootstrap tabs to indicate content is loading via ajax

Using Bootstrap 3 tabs and AJAX to load tab content can sometimes result in slower loading times. To address this issue, it would be beneficial to incorporate a spinner within the tab content to indicate that the server is processing the request. Is there ...

What could be the reason for my ajax request coming back with no data?

When I make this ajax request: $.ajax({ type: "POST", url: "/admin/RoutingIndicators/Add", data: { newSecondaryRI: newRi }, success: function () { alert('hit'); document.location.reload(true); }, error: fu ...

Undefined global variable

Within my function, I have defined a global variable called window.playerLibrary. Interestingly, when I check the value of window.playerLibrary within the function itself (`var check #1`), it returns a value. However, if I try to check it just outside of t ...

Tips for improving performance on AJAX-based websites with unreliable networks

During my recent travels, I have come across an issue with the way Ajax constructs websites. While I understand that requesting only necessary pieces of a webpage is efficient for servers, in areas with intermittent or limited signal, sites using this mode ...

Tips for testing nested HTTP calls in unit tests

I am currently in the process of unit testing a function that looks like this: async fetchGreatHouseByName(name: string) { const [house] = await this.httpGetHouseByName(name); const currentLord = house.currentLord ? house.currentLord : '957'; ...

Is the reference to a variable within an array maintained by JavaScript?

I'm working with react code and using the find() method to select an item from an array. When I retrieve an item from the array, is JavaScript copying the item or returning a reference? EDIT: The items in my array are objects, such as [{id: 12, name ...

Generate an image instantly from text using ajax when a key is pressed

Currently, I am immersed in a stencil project where my goal is to convert text into an image. In this particular task, there's a textbox that captures the user input on key up event. Once the user enters text, my aim is to display that text as an imag ...

How to fetch images from a database in CodeIgniter by utilizing JSON and AJAX functions?

Trying to retrieve an image using ajax/json format for the first time, all variables are displaying except the image. The name of the image is visible when inspecting the element in the browser and it is saving correctly into the image folder. I need help ...

Using the highcharts-ng library in combination with ng-repeat was successful in creating multiple charts. However, in order to display different data for each chart, the

I need to provide the date either from the view template or possibly from the controller in order for the highchart to display the data specified by the <highchart /> directive. Explanation : <ul> <li ng-repeat="li in list"> ...

Refresh the Datatable with the click of a button

Despite my efforts and multiple attempts at different solutions, I am unable to get this particular issue resolved. It seems that my lack of experience with javascript/UI is hindering me from achieving the desired outcome. The background function in quest ...

Operating with a multidimensional entity

I am aiming for an object structure like this: {"Red 1":53,"Blue 2":26,"Green 3":25} Based on the following example: I attempted to push data from within .each loop into the object. However, due to its multidimensional nature, I'm uncertain how to ...

Question about React.js: What is the best way to add multiple classes to a component by using a ternary operator?

I am looking to apply multiple classes to a component by utilizing a ternary operator. In our shared ts theme file, we have the general button styles defined, but for this specific component, I want to adjust the sizes based on screen width. To achieve thi ...

Using jQuery validation to verify that a minimum of one radio button holds a true value

I have a form with two questions. The first question asks if the product value exceeds a certain fixed amount, and the second question asks if the product value is below that fixed amount. Upon submitting the form, validation should ensure that at least on ...

Get a file from a distinct internal server than the one where the website is operating

I am trying to download a file from an internal server that is separate from the one where the website is hosted. I have attempted various methods such as... <a href="javascript:Start('file://servername/path/filename.txt')> <a href="// ...

Encountering a "400 Bad Request" error when using Ajax within WordPress

I've been trying to submit a form using Ajax within a plugin. I had two plugins, the first one was initially working but has stopped now and I can't seem to find any errors. I don't think the issue lies in the code itself, but I'm feeli ...

Finding all parent IDs from a given child ID within a nested JSON structure that contains children can be achieved by recursively

function loadKendoTreeView() { if ($("#treeview").data("kendoTreeView") != null) { $("#treeview").data("kendoTreeView").destroy(); $("#treeview").empty(); } var jsonData = [{ "Id": "239297d8-5993-42c0-a6ca-38dac2d8bf9f", ...

Utilizing the power of Angular 4 in combination with mailgun

I need assistance with setting up an email form on my website using Angular 4 and Mailgun as the mail service. I have a method in my mail service file to send messages, but I keep encountering a Bad Request error stating that 'from' is not presen ...