0

I'm creating a script which is going to automatically receive data from API and store it in MongoDB at specific UTC time.

I use Node-Schedule for scheduling a task at specific time. CoinMarketCap API for receiving a real time data.

Problem: I receive an undefined in console every second(since the node-schedule is configured to call the code every second). I was looking for any syntax errors or errors in API and wasn't successful. I understand that I receive undefined cause the function doesn't return anything. Currently doesn't have any ideas what wrong with it at all. All API keys and DB username password was correct I checked it as well.

Goal: To have the script which is automatically receives data from API and stores it MongoDB collection.

Full Code

const { MongoClient } = require('mongodb');
const schedule = require('node-schedule');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

const saveToDatabase = function(BTCdata) {
    const url = 'mongodb+srv://name:[email protected]/<dbname>?retryWrites=true&w=majority';

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, db) => {
        if (err) throw err;
        const dbo = db.db('Crypto');
        const myobj = { Name: 'BTC', Volume: 'BTCdata' };
        dbo.collection('Crypto-Values').insertOne(myobj, (error, res) => {
            if (error) throw error;
            console.log('1 document inserted');
            db.close();
        });
    });
};

function request(method, url) {
    return new Promise(((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open(method, url);
        xhr.onload = resolve;
        xhr.onerror = reject;
        xhr.send();
    }));
}

const j = schedule.scheduleJob('* * * * * *', () => {
    request(
            'GET',
            'http://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=API-KEY-HERE',
        )
        .then((r1) => {
            const x1 = JSON.parse(r1.target.responseText);

            const BTCdata = x1.data.find((d) => d.symbol === 'BTC').quote.USD.volume_24h; // creating a variable to store a BTC request from API


            console.log(BTCdata);
            // Saving to database
            saveToDatabase(BTCdata);
        })
        .catch((err) => {
            console.log(err);
        });
});

EDIT1: This is a console log of x1 value. x1 Console Log EDIT2: Was missing this part -

var request = require('request');

After it was added I start receiving a new error in my console which is :

events.js:287
      throw er; // Unhandled 'error' event
      ^

Error: Invalid URI "GET"
at Request.init
at new Request
at request
at Job.job
at Job.Invoke
at Users/path to node modules/node-schedule
at Timeout.onTimeout

EDIT3: After correction to the code with @Sureshprajapati answer. New error appears - TypeError: Cannot read property 'responseText' of undefined Trying to find solution by myself. Still looking for any advice. Thank you.

var requestPromise = require('request-promise');

requestPromise.get({
  uri: 'http://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=API-KEY-HERE',
  json: true
}).then(r1 => {
  const x1 = JSON.parse(r1.target.responseText);

  const BTCdata = x1.data.find(d => d.symbol === 'BTC').quote.USD
    .volume_24h; // creating a variable to store a BTC request from API

  console.log(BTCdata);
  // Saving to database
  saveToDatabase(BTCdata);
}).catch(err => {
  console.log(err);
});
6
  • 1
    What is x1 value? Can you add a log and check? Commented Jun 9, 2020 at 6:53
  • @SureshPrajapati Main question edited. This is a array of all cryptocurrencies from CoinMarketCap. Screenshot attached, you can look. Commented Jun 9, 2020 at 7:04
  • Can you see if your record is present in the Array with exact structure and expected keys? Commented Jun 9, 2020 at 7:21
  • @SureshPrajapati Yes, it was working before i start adding scheduling for script. As well with that I forgot to add require request. After I add require request I start receiving a new error - "Invalid URI "GET"". Main question edited with new error. Commented Jun 9, 2020 at 7:23
  • Did you mean var request = require('request-promise'); ? Commented Jun 9, 2020 at 7:27

1 Answer 1

1

request supports both streaming and callback interfaces natively. If you'd like request to return a Promise instead, you can use an alternative interface wrapper for request. These wrappers can be useful if you prefer to work with Promises, or if you'd like to use async/await in ES2017.

request-promise (uses Bluebird Promises)

This module is installed via npm:

npm install --save request
npm install --save request-promise

Coming to modifying your code as per documentation:

var requestPromise = require('request-promise');

requestPromise.get({
  uri: 'http://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=API-KEY-HERE',
  json: true
}).then(x1 => {

  const BTCdata = x1.data.find(d => d.symbol === 'BTC').quote.USD
    .volume_24h; // creating a variable to store a BTC request from API

  console.log(BTCdata);
  // Saving to database
  saveToDatabase(BTCdata);
}).catch(err => {
  console.log(err);
});
Sign up to request clarification or add additional context in comments.

1 Comment

it works for me. Thank you so much for explaining difference. Upvoted, confirmed for correct answer! Thanks!

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.