Reputation: 13
I'm using node js with mongodb. I'm trying to insert an element in the Collection "users" created previously. Afterwards, I delete the Collection and insert two new "users" and print them.
However, the code isn't being executed in the order I was expecting.
I've tried a few different things. For example, removing dropCollection makes it execute in the desired order but I need to use this function for my code. I also tried not using await and obviously, the same result appears.
var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, {useNewUrlParser:true}, async function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var myobj = { email: "[email protected]", name: "example"};
await dbo.collection("users").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 user inserted");
});
await dbo.dropCollection("users", function(err, delOK) {
if (err) throw err;
if (delOK) console.log("Collection deleted");
});
var myobj = { email: "[email protected]", name: "example"};
await dbo.collection("users").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 user inserted");
});
var myobj = { email: "[email protected]", name: "example2"};
await dbo.collection("users").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 user inserted");
});
dbo.collection("users").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
})
The output I am expecting:
$ npm start
1 user inserted
Collection deleted
1 user inserted
1 user inserted
[{ _id: 1234,
email: '[email protected]',
name: 'example' },
{ _id: 5678,
email: '[email protected]',
name: 'example2' }]
But the output I get is:
$ npm start
1 user inserted
1 user inserted
Collection deleted
[]
1 user inserted
Upvotes: 1
Views: 858
Reputation: 6058
You can await
only on Promise
object. Remove all your callback functions within async
context.
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, {useNewUrlParser:true}, async function(err, db) {
if (err) throw err;
const dbo = db.db("mydb");
var myobj = { email: "[email protected]", name: "example"};
await dbo.collection("users").insertOne(myobj);
console.log("1 user inserted");
const delOK = await dbo.dropCollection("users");
if (delOK) console.log("Collection deleted");
var myobj = { email: "[email protected]", name: "example"};
await dbo.collection("users").insertOne(myobj);
console.log("1 user inserted");
var myobj = { email: "[email protected]", name: "example2"};
await dbo.collection("users").insertOne(myobj);
console.log("1 user inserted");
const result = await dbo.collection("users").find({}).toArray();
console.log(result);
db.close();
})
Upvotes: 2
Reputation: 2944
MongoClient uses callbacks rather than Promises so your awaits are currently not doing anything. You can solve this by wrapping the Mongo calls in a Promise and manually resolving them in the callback e.g.
const createUser = async userObj => {
return new Promise((resolve, reject) => {
dbo.collection('users').insertOne(userObj, function(err, res) {
if (err) reject(err);
resolve('1 user inserted');
});
});
};
// ...
await createUser();
Upvotes: 1