0

I'm working on some code for an express API that essentially reaches out to an external REST service for an authentication token, and then uses that token to do stuff. I'm very new to NodeJS, so I think I'm having a lot of trouble with the whole sync/async thing.

Right now my main problem seems to be that I'm getting ReferenceError: err is not defined, which seems to be related to the line in library.js, but I expect there are a lot of problems here, and will appreciate anything that can get me back on the right track.

index.js

library = require('library.js');

module.exports = async (req,res) => {
    // This is a test endpoint for prototyping code and testing calls.
    URI = "/some/uri";
    method = "GET";
    body = "";
    try {
        restResponse = await library.RESTCall(URI,method,body);
        res.send(data);
    } catch (e) {
        return res.status(500).json({ errors: err});
    }
};

library.js

exports.RESTCall = async function(URI,method,body) {
    return new Promise((resolve, reject) => {
        getAuthToken().then((token) => {
            console.log("Token: " + token);
            try {
                // Do stuff with the token to make another call
                resolve(data);
            } catch (e) {
                reject(e);
            }
        }).catch((err) => {
            reject(err);
        });
    });
}

exports.getAuthToken = () => {
    return new Promise((resolve, reject) => {
        try {
            // Do stuff to get an authentication token
            resolve(authToken);
        } catch(e) {
            reject("Failed to get Facets Authentication token. Error: " + e);
        }
    });
}
Crackerjam
  • 71
  • 1
  • 8
  • 2
    In index.js you have `json({ errors: err}` where err is undefined because you use `catch(e)` – Rob Bailey Nov 13 '20 at 17:05
  • `return res.status(500).json({ errors: err})` => `return res.status(500).json({ errors: e})` – messerbill Nov 13 '20 at 17:08
  • 1
    [You are using the explicit promise constructor antipattern!](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – VLAZ Nov 13 '20 at 17:10

1 Answers1

1

This looks like just a typo:

    return res.status(500).json({ errors: e});

FYI this:

exports.RESTCall = async function(URI,method,body) {
    return new Promise((resolve, reject) => {
        getAuthToken().then((token) => {
            console.log("Token: " + token);
            try {
                // Do stuff with the token to make another call
                resolve(data);
            } catch (e) {
                reject(e);
            }
        }).catch((err) => {
            reject(err);
        });
    });
}

Is mostly equivalent, but slightly worse as:

exports.RESTCall = function(URI,method,body) {
   return getAuthToken().then((token) => {
     console.log("Token: " + token);
     // Do stuff with the token to make another call
     return data;
   }
}

But because you have async/await, can be simplified further:

exports.RESTCall = async function(URI,method,body) {
   const token = await getAuthToken();
   console.log("Token: " + token);
   // Do stuff with the token to make another call
   return data;
}

Every time you see yourself type new Promise, consider it a red flag. I'd really suggest you take the time to learn how promises work.

Evert
  • 93,428
  • 18
  • 118
  • 189