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