Hexycode
Hexycode

Reputation: 458

Automatically insert data to MongoDB with NodeJS

I'm trying to create a script which is going to automatically send data from API every day at specific time and store it in MongoDB collection. For that purpose I use NodeJS-Schedule.

I just started to exploring MongoDB and now I'm looking for a way to automatically receive data from API and store it in MongoDB.

I saw that there is MongoDB-Cron

This is part of the code that I use to receive a data from API.

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

            var 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);
        })
        .catch((err) => {
            console.log(err);
        });
});

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

Here is the part to insert a data to MongoDB collection.

I a little bit don't understand how I can automate this insert function and can I make this automatisation to send data to mongodb every day at specific time?

var url = "mongodb+srv://name:[email protected]/<dbname>?retryWrites=true&w=majority";

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

EDIT1: Is the code supposed to be like this?

var MongoClient = require('mongodb').MongoClient;
var MongoCron = require('mongodb-cron');

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

    MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function(err, db) {
        var j = schedule.scheduleJob("*/55 20 * * *", function() {
            request(
                    "GET",
                    "http://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=API-KEY-HERE"
                )
                .then((r1) => {
                    var x1 = JSON.parse(r1.target.responseText);

                    var 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);
                });
        });
    });
};

Upvotes: 1

Views: 1309

Answers (1)

Tunmise Ogunniyi
Tunmise Ogunniyi

Reputation: 2573

I don't think you need to disturb yourself with automating the db insertion, you can include the insertion in the corn job itself. Perhaps, encapsulate the data persisting logic in a function:

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

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

You should call the function with the BTCData after the API call in the corn job. The program should be something like this:

const { MongoClient } = require('mongodb');
const schedule = require('node-schedule');

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('*/55 20 * * *', () => {
  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);
    });
});

Remember to catch errors where necessary

Upvotes: 1

Related Questions