ReferenceError: "db is not defined" when calling a function
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
// Connection URL
var url ='mongodb://localhost:27017'
// Database Name
var dbName = 'Sudent';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client){
if(err){console.log(err)}
else{console.log('connected server successfully')}
var db = client.db(dbName)
var collection = db.collection('record');
var doc1 ={name:"SAPAN", age:"40", Skill: "Mongodb"};
var doc2 ={name:"JOHAN", age:"45", Skill: "ES2016"};
var doc3 ={name:"PETER", age:"50", Skill: "NodeJs"};
// Insert some documents
collection.insertMany([doc1, doc2, doc3], function(err, res){
if(err){
console.log(err)
}else{
console.log(`%d documents inserted`, res.insertedCount)
}
client.close();
})
});
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
// Connection URL
var url ='mongodb://localhost:27017'
// Database Name
var dbName = 'Sudent';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client){
if(err){console.log(err)}
else{console.log('connected server successfully')}
var db = client.db(dbName)
var collection = db.collection('record');
var doc1 ={name:"SAPAN", age:"40", Skill: "Mongodb"};
var doc2 ={name:"JOHAN", age:"45", Skill: "ES2016"};
var doc3 ={name:"PETER", age:"50", Skill: "NodeJs"};
// Insert some documents
collection.insertMany([doc1, doc2, doc3], function(err, res){
if(err){
console.log(err)
}else{
console.log(`%d documents inserted`, res.insertedCount)
}
client.close();
})
});