0

I am doing a e-commerce aggregator site for an school project and I am looking to collate products from 3 separate APIs that would retrieve data from e-commerce sites such as Aliexpress or Amazon before placing it into 1 homepage.

Currently, I am able to retrieve the results from each individual APIs but am unable to store the results into a collated_results_list. When I try to return or append the individual_results to the collated_results_list, it returns undefined or does not append any individual_results to the collated_results_list.

My school project is based on Javascript and AJAX but is also able to use other libraries as long as they do not perform the same function as JS and AJAX. Would really appreciate it if anyone could help me out. Thank you!

The following code block is the pseudocode for my script in general.

//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;
                          // Expected output: taobao_list = taobao_product_list;
    
    //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;
}
  • But I don't see where those returned values were defined – Aggestor Oct 27 '20 at 04:15
  • So sorry, I'm so using to chaining methods. What i meant was call_amazon_api() is supposed to return a result inside of the collated_results_lists such as collated_results_list.concat(amazon_product_list) , but it returns as collated_results_list.concat(undefined). So overall the concatenation operation should work alright. – CodeSleepRepeat Oct 27 '20 at 04:20
  • I have updated my code block. Hopefully its much more clear now. – CodeSleepRepeat Oct 27 '20 at 04:34
  • To the person who answered my question, thank you so much. I finally understood why is was happening now. Thank you! – CodeSleepRepeat Oct 28 '20 at 06:20

1 Answers1

0

As per your code. This should not throw an error. I would suggest to debug the following part of code.

function call_amazon_api(){
    console.log("amazon_product_list" + amazon_product_list); //if this works, the data will pop on console
    return amazon_product_list; //results can be printed within call_amazon_api, but cannot be passed to 
                               //another global variable
}

function call_aliexpress_api(){
    console.log("aliexpress_product_list "+aliexpress_product_list); //if this works, the data will pop on console
    return aliexpress_product_list;
}

function call_taobao_api(){
    console.log("taobao_product_list " +taobao_product_list); //if this works, the data will pop on console
    return taobao_product_list;
}

Obviously, this part of your code is making problem.

Please let me know if it helps.

Asad Mehmood
  • 292
  • 2
  • 10
  • 20
  • Hmm, I did try to do a console.log() and the individual call_api functions are able to display the data at the part that you had mentioned. But thank you for your response! – CodeSleepRepeat Oct 27 '20 at 05:52
  • then try calling the list directly rather than calling functions. like `var x = amazon_product_list` rather than `var x = call_amazon_api()` and print it to console again. Hope this solve your porblem – Asad Mehmood Oct 27 '20 at 06:15
  • I just tried that as well. But thanks for your input! Looks like this bug is harder to fight than CoVid @.@ – CodeSleepRepeat Oct 27 '20 at 12:04