1

I'm starting to learn Node.js and I am trying to build an app with the Express framework that will query the Coinbase API and get data on the price of Bitcoin.

I have been struggling with one thing though and because I am new to callback functions on node it does not help.

I am able to fully query the API and get the data but when it comes to somehow returning the price, it always comes back as "undefined".

I had this problem previously with simple queries that did not use callbacks so I assumed it was to do with the lack of the callback. However, I am still getting the same problem now and I have tried numerous iterations. I can't seem to find a solution. Any help would be appreciated.

var url = 'https://api.coinbase.com/v2/prices/spot?currency=USD';

function makeCall (url, callback) {
    https.get(url,function (res) {
        res.on('data', function (d) {
            callback(JSON.parse(d));
        });
        res.on('error', function (e) {
            console.error(e);
        });
    });
}

function handleResults(results){
    return Number((results.data.amount))*14.5;
}

console.log(makeCall(url, function(results){
	handleResults(results);      
}));

1 Answer 1

1

makeCall() doesn't actually return anything. So when you call:

console.log(makeCall(url, function(results){
    handleResults(results);      
}));

you are writing the immediate return value of makeCall() (which is undefined) to the console. The return from your handleResults() happens too late for the console.log() to get.

It take a little getting used to. But you need to make sure when you need a value from an async callback, you wait to access it. For example this would work:

function handleResults(results){
    console.log(Number((results.data.amount))*14.5;)
}

Learning to use promises can makes some of this more intuitive and easier to read.

Using promises you could write it like:

const https = require('https')
var url = 'https://api.coinbase.com/v2/prices/spot?currency=USD';

function makeCall (url) {
    return new Promise((resolve, reject) => {
        https.get(url,function (res) {
            res.on('data', function (d) {
                resolve(JSON.parse(d));
            });
            res.on('error', function (e) {
                reject(e)
            });
        });
    })

}

function handleResults(results){
    return Number((results.data.amount))*14.5;
}

makeCall(url)
.then(function(results){
    console.log(handleResults(results))
})
.catch(console.log)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the advice @Mark_M. Will try to us promises going forward as it seems to be a favoured option. I am just struggling with the code in that I need to return a price as a number. I want to save it as a variable for example so that I can run other calculations on the price. For example, I would like to set results.data.amount from the makeCall API call equal to some variable, say "price" which is returned?
I hear you, it's confusing at first. You generally can't return things from callbacks and expect them to work because at some point you reach the originating function that has already run. You need to pass async values to other callbacks or use promises.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.