I am using nodeJS and mongodb in one of my project.
I am trying to save data in multiple collection in one save button.
The code which I am using to achieve this is as follow:
var lastInsertId;
loginData={
userName: req.body.txtUsername,
password: req.body.txtPassword,
active:1,
createdOn:new Date(),
updatedOn:new Date()
};
var dbo = db.db("test");
dbo.collection("login").insertOne(loginData, function(err, result) {
if (err) throw err;
lastInsertId=result.insertedId;
});
if(lastInsertId){
usersData={
email: req.body.txtEmail,
firstName: req.body.txtFirstName,
lastName:req.body.txtLastName,
mobileNumber:req.body.txtMobileNumber,
login_id:lastInsertId,
active:1,
createdOn:new Date(),
updatedOn:new Date()
};
dbo.collection("users").insertOne(usersData, function(err, result) {
if (err) throw err;
console.log('saved to users');
});
}
Could you please tell what is wrong in the above code?
Thank you.
Regards, Saloni