1

I am trying to fetch API data using Node JS. I am using this node package to do so.

https://www.npmjs.com/package/cryptocompare

The documentation of that package is easy enough.

global.fetch = require('node-fetch')
const cc = require('cryptocompare')

cc.price('BTC', ['USD', 'EUR'])
.then(prices => {
  console.log(prices)
})
.catch(console.error)

I've tested it with npm.runkit.com and it works.

However, when I install the package into my app, I don't see any output in the console.

I am using JetBrains WebStorm and these are the steps I've taken.

  1. Create New Express App
  2. npm install -g express-generator
  3. npm install --save node-fetch
  4. npm install --save cryptocompare
  5. npm install

Then within /routes/index.js I added the following

var express = require('express');
var router = express.Router();
global.fetch = require('node-fetch');
const cc = require('cryptocompare');

/* GET home page. */

cc.price('BTC', ['USD'])
    .then(prices => {
        console.log(prices)
}).catch(console.error);

router.get('/', function(req, res, next) {
    res.render('index', {
      title: 'Example'
    });
});

module.exports = router;

But that displays nothing in the console log. I tried moving the global.fetch to app.js in the root directory but that didn't do anything either.

What am I doing wrong here?

4
  • Try getting rid of the square brackets in your second example. The documentation doesn't have them. Commented Jul 31, 2017 at 15:55
  • What square brackets? the ones around USD ? Commented Jul 31, 2017 at 15:58
  • mmhm it's different from the example under "passing a single pair of currencies" Commented Jul 31, 2017 at 16:00
  • It's all the same, you can still pass 1 item in an array. In any case, removing the square brackets doesn't do much. Commented Jul 31, 2017 at 16:01

2 Answers 2

2
    var express = require('express');
    var router = express.Router();
    global.fetch = require('node-fetch');
    const cc = require('cryptocompare');

    /* GET home page. */


    router.get('/', function(req, res, next) {
cc.price('BTC', ['USD'])
        .then(prices => {
    res.render('index', {
          title: prices
        });
    }).catch(console.error);

    });

    module.exports = router;

This will work for you

Sign up to request clarification or add additional context in comments.

3 Comments

This returns [object Object] for the title
Fantastic. I am new to the MEAN stack. I really appreciate your patience.
@Halnex, No Problem. if you want i can email you a book which is really nice and easy to understand. Let me know if you need.
1

Not sure why you are not getting anything. I tried with same steps and got the result.

I just did one thing differently, i pasted whole code in a file named as abc.js.

and then i ran it in command line like this

node abc.js

and i got this result

{ USD: 2797.06 }

Can you please try it again because its working awesome for me. Let me know if you face any problem.

Continue...

So if you want to use it in index.js then you can do something like this

cc.price('BTC', ['USD'])
    .then(function(prices){
        console.log(prices)
}).catch(function(error){
console.log(error);
});

I just changed its syntex from es6 to es5

6 Comments

Yes, I just tested this and it works. But I would like to pass the data to index.js route instead and eventually be able to print out the data on the page instead of the console.
@Halnex Oh, wait, I assumed you were looking at your IDE's console. You are fetching the data outside of the router, which means this is happening immediately as your server starts running. If you move the fetch call to inside the router and render inside the fetch callback you should see the data in your browser's console.
@spicypumpkin I've been trying to move it inside the router and render on index.js but the IDE keeps throwing syntax errors. I can't move the fetch snippet inside an array where the title is.
@Halnex What array? You should be able to access the prices through closure or as an object property. Can you tell me where the syntax error is? Maybe edit it into your original question and I can post a legitimate answer.
res.render('index', { title: 'Example' }); This array. Let's say I want to print the price instead of Example - passing cc.price('BTC', 'USD') returns [object Promise] - I am new to the MEAN stack, I would appreciate an example.
|

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.