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.
- 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.
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); }); });
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.