Exploring the concept of making nested API requests within a route using Node.js and

Being a newbie in the world of coding, I'm excited to make my first post here. My current learning project involves developing a website that utilizes an external CRM for storing client data received from web forms.

The storage functionality is up and running smoothly, but I'm facing challenges when it comes to fetching the data and passing it to a rendered page. I require a route to carry out three operations sequentially, each working fine individually but struggling to nest them effectively.

  1. Obtaining details of a deal from the CRM
var options = { method: 'GET',
                    url: 'https://crm.com/dev/api/opportunity/' + req.params.id,
                    headers: 
                        { 'cache-control': 'no-cache',
                         'content-type': 'application/json',
                          accept: 'application/json',
                          authorization: 'Basic xxx' },
                          json: true };
            request(options, function (error, response, body) {
                if (error) throw new Error(error);
                    return body.contact_ids;
            });

This request will provide an array of client numbers associated with the deal.

  1. Iterating through the client numbers to retrieve data from each client and store it in an array. I have initialized an empty array named data outside the function scope to capture the results.

         resultFromAboveRequest.forEach(function(id) {               
            var options = { method: 'GET',
                 url: 'https://crm.com/dev/api/contacts/' + Number(id),
                 headers: 
                  { 'cache-control': 'no-cache',
                     'content-type': 'application/json',
                     accept: 'application/json',
                     authorization: 'Basicxxx' },
                    json: true };
    
             request(options, function (error, response, body) {
    
                if (error) throw new Error(error);
                data.push(body);
            });
        });
    
  2. Displaying the resulting data array on a webpage

    res.render("./applicants/resume", {data: data});

I believe utilizing promises would be ideal for this task, but I am having difficulty grasping the syntax. Any assistance would be greatly appreciated. My apologies if the format of this question appears amateurish or inappropriate in any way.

Answer №1

If you want to efficiently manage a series of asynchronous operations with proper sequencing and error handling, I recommend utilizing the request-promise library. This library serves as a promise-based interface to the standard request library, allowing for smoother operation flow.

const rp = require('request-promise');

const options = {
    method: 'GET',
    url: 'https://crm.com/dev/api/opportunity/' + req.params.id,
    headers: {
        'cache-control': 'no-cache',
        'content-type': 'application/json',
        accept: 'application/json',
        authorization: 'Basic xxx'
    },
    json: true
};
rp(options).then(body => {
    return Promise.all(body.contact_ids.map(id => {
        const options = {
            method: 'GET',
            url: 'https://crm.com/dev/api/contacts/' + Number(id),
            headers: {
                'cache-control': 'no-cache',
                'content-type': 'application/json',
                accept: 'application/json',
                authorization: 'Basicxxx'
            },
            json: true
        };
        return rp(options);
    }));
}).then(data => {
    res.render("./applicants/resume", {data: data})
}).catch(err => {
    console.log(err);
    res.status(500).send("internal server error");
});

To break down the process:

  1. Integrate the request-promise library which returns promises instead of completion callbacks, ensuring better management of async tasks.
  2. Initiate the first request using .then() handler to extract the outcome.
  3. Utilize .map() to process the received result effectively.
  4. In each iteration of .map(), return another promise by calling request-promise accordingly to generate an array of promises.
  5. Employ Promise.all() on this array of promises for synchronization purposes.
  6. Chaining the returned promise from Promise.all() ensures sequential execution post previous actions' completion.
  7. Implement another .then() to access ordered data from .map() for rendering via res.render().
  8. For error mitigation, incorporate a .catch() block to capture any issues within the promise chain, redirecting them to trigger appropriate error responses.

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

Issue occurred: The error "Undefined offset 1" was encountered while trying to upload a file via

Every time I try to pass data into my file upload controller, I keep encountering an error message that says undefined offset: 1. function TestFileUpload() { $i=0; if(!isset($_FILES[$i]) ) { echo "No file is being uploaded"; } el ...

What steps can be taken to fix a syntax error in a NodeJS express server code?

I am currently facing a syntax error in the code below, and I'm struggling to fix it. The specific error message is as follows: node staticapi.js /Users/v/Desktop/CS-Extra/EIP/A5/staticapi.js:123 res.status(200).send("Api is running") ...

How can you loop through keys and values in a JSON object using JavaScript?

I am attempting to cycle through the JSON data below: { "VERSION" : "2006-10-27.a", "JOBNAME" : "EXEC_", "JOBHOST" : "Test", "LSFQUEUE" : "45", "LSFLIMIT" : "2006-10-27", "NEWUSER" : "3", "NEWGROUP" : "2", "NEWMODUS" : "640" } The keys in this JSON are d ...

``Is there a way to retrieve the file path from an input field without having to submit the form

Currently, I am looking for a way to allow the user to select a file and then store the path location in a JavaScript string. After validation, I plan to make an AJAX call to the server using PHP to upload the file without submitting the form directly. Thi ...

Performing a Search Operation using AJAX with Elasticsearch

I've been struggling to find the correct method for requesting data from elasticsearch using a jQuery AJAX call. I keep encountering parsing errors or getting all documents in the index instead of my intended results. $(document).ready(function() ...

AngularJS making a HttpPost request resulting in a 500-Internal Server Error

I'm currently working on an application where I need to include a user in the database which requires a POST operation. However, every time I try to run the application, I encounter a 500-Internal Server Error for the POST API call. Here is a snippe ...

Guide on establishing a connection to a broker API using Java and sockets?

I've been struggling to establish a connection with the XTB API and it's becoming quite frustrating. I have very limited knowledge when it comes to sockets, so I'm basically learning as I go along. My current challenge involves sending a JSO ...

Issue: Headers cannot be modified once they have been sent

choose_option: function (remove) { var myself = this; myself.$http.post("/inventory/manager_id_list", {"id": remove,}).then(function(response){ var dataParent = response.body.result[0]; },function(failure){ ...

Order an array in Javascript based on the day of the month

I am trying to create a unique function that changes the order of a string based on the day of the month. For instance, if the input string is "HELLO" and today's date is the 1st of April, the output should be "LHEOL". On the 2nd of April, it should ...

A guide on transferring and transforming text data on the server

While I have a basic understanding of php, ajax, and javascript (including jQuery), I am still a beginner and could use some assistance with connecting the dots for this simple task: My goal is to allow the user to input text (for example: "I saw the sun, ...

Building a custom onChange event handler in Formik allows for greater

I want to modify the onChange function in formik input so that it converts the value from a string to a number. However, I'm unable to change the behavior as expected and the console.log doesn't show up on the screen. It seems like Formik's ...

GeoJson with timestamp and marked directional indicator

I am looking to create an animation of a boat traveling along a specific route. Currently, I am able to display the route as a line and the boat as a circle using TimestampedGeoJson: # circle with following line features = [ { 'type': ...

The jQuery div enclosure technique

I am trying to wrap HTML around an existing div, here is what I have attempted: HTML: <input type="text" data-placeholder="username" /> It should look like this when rendered: <div class="placeholding-input"> <input type="text" data-pl ...

The server will still send a success response even if it was unable to locate any results

When I submit data from my input fields to my API: $.ajax({ url: '/api/login', type: 'GET', dataType: 'json', ContentType: 'application/json', data: {formData}, succes ...

When using $.getJSON and $.ajax, the expected JSON object is not being returned

Currently, I am taking on the "local weather" front-end development challenge on freecodecamp.com. However, I'm facing some challenges when it comes to making an API call to fetch weather data from various weather APIs. This particular task requires r ...

Is there a way to turn off alerts from Aspx files for HTML and CSS?

Dealing with annoying warnings in my aspx files has been a constant struggle. The "CSS Value is not defined" message pops up when I reference CSS files from different projects, causing unnecessary frustration. Even more frustrating are the warnings about i ...

Using jQuery JSONP only functions with URLs from the same site

I am looking to send a request to an API that returns data in json format. The API is located on a sub-domain of the domain where my script will be running (although currently it's on a completely different domain for development, localhost). My unde ...

Could use some help with configuring express routes with parameters

Greetings! I am new to working with Express and I am currently in the process of creating a portfolio website. In my project, I have a Pug file named project.pug which includes HTML content that should dynamically render based on the ID of each project sto ...

Analyzing the structure according to the month/week/year

My array consists of count and date values: day = [ { count: 1, date: '2022-07-07' }, { count: 1, date: '2022-08-14' }, { count: 2, date: '2022-07-19' }, { count: 4, date: '2022-07-19' }, { count: 2, date: ...

Execute an asynchronous function in Javascript, then output the returned data to the console

Is there a way to effectively handle the data returned from an async function? example: JS FILE: async function getData(){ try { $.getJSON('./data.json', (data) => { return data; }); } catch(error ...