Pass the output from the Unirest Node get request function to be displayed in the Jade view

I'm currently in the midst of developing my very first Node application, and I've hit a snag with unirest.get requests. My project features Node, Express, as well as the Act On API.

To speed up the setup process, I've opted to use the express generator.

The issue at hand is that I'm having difficulty passing the response to my route file. While I can clearly see the correct response from the Act On API in the console log, I'm unable to access the data within the templates.

function getTheList(callback) {
var Request = unirest.get('https://restapi.actonsoftware.com/api/1/list/l-0001')
.headers({
    'Accept': 'application/json',
    'Authorization': 'Bearer ' + access_token
})
.query({
    "count": 20,
    "fields": "First Name;Last Name;Email;"
})
.end(function(response, error) {
    var data = response.body.data;
    if (!error && response.statusCode == 200) {
        callback(returnData(data)); 
    } else {
        console.log('Failed response');
    }
});
}

function returnData(theData){
  console.log(theData);
  return theData;
}

module.exports.get = getTheList;

Within my routes file, you'll find the following code snippet used to retrieve this information:

var masterList = require('../acton/getMasterList');

var myListVar = masterList.get();

Any assistance on identifying where I may be going wrong would be tremendously helpful.

Answer №1

When you use the masterList.get() function without providing a callback, as described in your question, it will not work as expected.

To fix this issue, you have a couple of options:

masterList.get(function(data){
    //you can access data here.
})

Alternatively, you can modify the implementation of the getTheList function to remove the need for a callback entirely.

 .end(function(response, error) {
    var data = response.body.data;
    if (!error && response.statusCode == 200) {
      returnData(data); //Simply do this instead.
    } else {
      console.log('Failed response');
    }
});

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

What is the method for writing the following line in CSHTML server-side code?

<script> function a(id) { var table = document.getElementById(id); .... } </script> @{ //Is there a way to rewrite the line "var table = document.getElementById(id)" here within the ser ...

Obtain the breakpoint value from Bootstrap 5

We have recently updated our project from Bootstrap 4 to Bootstrap 5. I am trying to retrieve the value of a breakpoint in my TypeScript/JavaScript code, which used to work in Bootstrap 4 with: window .getComputedStyle(document.documentElement) .g ...

Unable to Display Embed Request Using Javascript in IE9 and IE10

My website allows users to embed content they create on the site into their own blogs or pages using a series of embeds. Here is the code we provide them: <script src="[EMBED PROXY URL]" type="text/javascript"></script> When this code calls ...

Utilizing HTML types in a custom hook with React and Typescript

Is it possible to pass type annotations like as SVGElement or as HTMLDivElement into a hook? function AppSVG(){ const ref = useResizeObserver((entry) => { ... }) as SVGElement;// <- How can the SVGElement be passed to the hook? r ...

What is the process of compiling TypeScript code?

When attempting to use tsc, I encountered issues. Even when having typescript but lacking tsc, the problem persisted. What steps should I take next? https://i.sstatic.net/Djgqb.png ...

Incorporating a React element into a JavaScript object's property: A comprehensive guide

Below is a React Element named Info that has been attached to a Javascript object named myObj: let Info = ( <Info type="green" /> ); let myObj = { ReactComp: Info }; Now, the goal is to render the Info component using the above myObj objec ...

Setting a displacement/normal map for only one face of a cylinder

My current setup involves creating a cylinder using the following code: var geometry = new THREE.CylinderGeometry( 50, 50, 2, 128 ); The resulting shape is a flat cylinder resembling a coin. However, when I apply a displacementMap and normalMap, I notice ...

Switch Background Image - The image failed to display

Here is the code snippet I am currently working with. It involves displaying a background-image when clicking on a link with the class 'home-link', and not showing an image if the link does not have this class. The issue at hand: The problem ari ...

Ways to showcase the content of a page once the controller variables have been set

As someone who is just starting out with AngularJS and web development, I am curious to know if there is a way to delay the display of a page until after all of the controller's $scope variables have been initialized. Most of my $scope variables are c ...

"Unlocking the Dialog Box: A Step-by-Step Guide to Programatically Opening Material UI Dialog

Typically, Material UI's Dialog is used as shown below, following the documentation: export default function AlertDialog() { const [open, setOpen] = React.useState(false); const handleClickOpen = () => setOpen(true); const handleClose = () =& ...

The functionality to disable the ES lint max length rule is malfunctioning

In trying to disable an eslint rule in a TypeScript file, I encountered an issue with a regular expression that exceeded 500 characters. As a result, an eslint warning was generated. To address this, I attempted to add an eslint comment before declaring th ...

Transferring data from Node.js (Express) server to iOS App using Swift 3

I'm currently working on implementing a login system for my iOS mobile app. I've set up a request to my Node.js server using Swift 3: @IBAction func loginBtn(_ sender: UIButton) { //created NSURL let requestURL = NSURL(string: loginURL) ...

Instructions for adding a name to a string based on certain conditions

I am attempting to prepend a company name to a card number using the code provided. The challenge I am facing is incorporating the specific rules for each company as conditions for an if statement. Although I acknowledge that my current approach with the ...

Display the designated element upon clicking the designated link exclusively

I'm working with this specific HTML setup: <a href="#" class="dp">Click me</a> <div class="dp_div" style="display: none;"> this is the content within the div </div> My goal is to display the hidden div with a class of "dp_ ...

React is producing a collection of <td>'s

My React code is very straightforward and it runs smoothly: function Columns(){ return ( <React.Fragment> <li>Hello</li> <li>World</li> </React.Fragment> ); } function Example(){ ...

The process of verifying a user's login status using Node.js and Mongoose

Is there a way to check if a user is logged in when accessing /profile using a command? When a user visits the home page "/", I want it to render either profile.ejs if they are logged in, or home.ejs for registration and login. However, I don't want ...

Maintain HTML formatting when page is refreshed

I am new to HTML and JavaScript, and I'm trying to modify the JavaScript code below so that when I reload the page, it retains the most recent active tab instead of defaulting back to the first tab. Currently, if I click on the second tab for "Paris" ...

What is the best way to merge multiple statements into one before passing them into a JavaScript method?

I am faced with several javascript statements like the ones listed below: $('#' + xxx_slot_name1).children().remove(); $('#' + xxx_ad_slot_name2).children().remove(); $('#' + xxx_ad_slot_name3).children().remove(); $('#& ...

I'm curious if there is a method to incorporate localStorage into the initialState of Redux Toolkit within Next.js 14

Attempting to establish the initial value of a Redux Toolkit slice for dark mode using localStorage is proving problematic in Next.js, as the window object is not defined on the server-side, resulting in errors. The typical workaround involves using if (t ...

Using Vue.js: Invoking a Function from a Different Component

Here is the HTML snippet from Component1 <component2></component2> This is the <script> block from Component1 export default { methods: { fetchData() { ... } } } Now, I want to call the method fetchData() in Compon ...