I am a beginner at Node and Mongo so I need some help. I am practicing with chaining promises and with MongoDB. I want to chain together 2 insert actions. However, when I run this code file, I get the error: "TypeError: Cannot read property 'db' of undefined at /Users/jenniferzhou/Documents/2018 - 2019/MEAN Practice/Guru99/E8/mongopractice5.js:12:22 at process.internalTickCallback (internal/process/next_tick.js:77:7) " The error says that the problem is with this line of the code: "var db = client1.db('EmployeeDB')". However, I am unsure why this line will return an error. I know that MongoClient.connect(url, { useNewUrlParser: true }) returns a promise but is this promise in some way different from the promise returned by the first .then(...)?
var Promise = require('promise')
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017'
MongoClient.connect(url, { useNewUrlParser: true }).then(function(client) {
var db = client.db('EmployeeDB')
db.collection('Employee').insertOne({
Employeeid: 4,
EmployeeName: "NewEmployee"
})
client.close()
}).then(function(client1) {
var db = client1.db('EmployeeDB')
db.collection('Employee').insertOne({
Employeeid: 5,
EmployeeName: "NewEmployee1"
})
client1.close()
}).catch(function(err) {
console.log(err)
})