So the thing is i'm getting data from blockchain and setting that data in my model in updateProgress() whenever there while be any change in blockchain i'll call my updateProgress() and it set data in model and then save it.
But here for saving the data to mongodb as u can see i have pass passdata from updateProgress() to mongodb.connect() and my database conncet is getting call only once so when blockchain is changing updateProgress() is getting call but data is not getting save.
So, how can i use db.collection() outside of mongodb.connect() i want to use it inside my updateProgress() so that every time it get called data will get set in model the save to database
Here if i want to save the data everytime i have to create conncetion with database
function updateProgress(message){
console.log('updateProgress is running ')
// db save;
const data = new datamodel({
type: message.type,
req_id:message.req_id,
// block_num:message.data.block_num
});
passdata=data;
}
const uri = "mongodb+srv://<databasename>:<password>@cluster0-z2ii6.mongodb.net/test?retryWrites=true&w=majority"
var dbconn= MongoClient.connect(uri,{ useNewUrlParser: true ,useUnifiedTopology: true},function(err, db) {
if(err) {console.log('Error occurred while connecting to MongoDB Atlas...\n',err);}
console.log('Connected...');
dbo=db.db(<databasename>)
dbo.collection('datamodel').insertOne(passdata,(err,passdata)=>{
if(err) throw err
console.log('data is inserted')
})
});