Answer the question
In order to leave comments, you need to log in
How to fix error in nodejs working with mongodb?
const mongo = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017';
mongo.connect(url, { useNewUrlParser: true }, (err, client) => {
if (err) {
console.log('Connection error: ', err)
throw err
}
console.log('Connected')
const db = client.db('pageLinks');
const allLinks = db.collection('allLinks');
allLinks.find({subcategory: 'subcategory2'}).toArray(function(err, results){
console.log(results);
});
client.close();
})
mongo.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {...})
allLinks.find({subcategory: 'subcategory2'}).toArray(function(err, results){
console.log(results);
});
Answer the question
In order to leave comments, you need to log in
It seems that the error with undefined has been fixed. client.close() was faster than allLinks.find() completed.
I ended up changing the line like this:
allLinks.find({subcategory: 'subcategory2'}).toArray(function(err, results){
console.log(results);
client.close();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question