MongoDB Node.js issue with "Callbacks are deprecated" - node.js

Hello I have issue with MongoDB code, it make some methods like this (image below):
enter image description here
`
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, { useUnifiedTopology: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("mydb");
var mysort = { name: 1 };
dbo.collection("customers").find().sort(mysort).toArray(function (err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
`
and when I try read error, it say:
(method) MongoClient.connect(url: string, callback: Callback): void (+3 overloads)
#deprecated — Callbacks are deprecated and will be removed in the next major version. See mongodb-legacy for migration assistance
The signature '(url: string, callback: Callback): void' of 'MongoClient.connect' is deprecated.ts(6387)
mongodb.d.ts(4733, 9): The declaration was marked as deprecated here.
No quick fixes available
I tried reinstall it, or install older version and still same, I do connection same like in documentation.
Tried add (url,{ useUnifiedTopology: true }, and still same
Someone know what can be issue?

Try version 4.0.0
const MongoClient = require('mongodb').MongoClient;
// Connect to the MongoDB server
MongoClient.connect('mongodb://localhost:27017/', function(err, client) {
if (err) throw err;
// Get the sample database
const db = client.db('sample');
// Get the collection
const collection = db.collection('test');
// Insert a document
collection.insertOne({ name: 'John', age: 30 }, function(err, result) {
if (err) throw err;
console.log(result);
// Close the connection
client.close();
});
});
Let me know if you still have the same error .

Related

Mongodb find not printing json data

In the code I am trying to find all documents with code UE19CS204.But in the console.log
a big message is printed not the JSON data.The findOne() is working but not find().
I don’t know what change to do to find all documents with code UE19CS204.
var MongoClient = require(‘mongodb’).MongoClient;
var url = “mongodb://localhost:27017/”;
MongoClient.connect(url, { useUnifiedTopology: true } ,function(err, db) {
if (err) throw err;
var dbo = db.db(“pes”);
dbo.collection(“course”).find({“code”:“UE19CS204”}, function(err, result) {
if (err) throw err;
console.log(result);
});
dbo.collection(“course”).findOne({code:“UE19CS204”}, function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
The method find() creates a cursor for a query that can be used to iterate over results from MongoDB, see here.
Use toArray(), you can finde the documentation here.
dbo.collection(“course”).find({“code”:“UE19CS204”}).toArray(function(err, docs) {
if (err) {
throw err;
}
console.log(docs);
})
Full example:
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'pes';
// Collection Name
const collectionName = 'course';
// Filter
const filter = { 'code': 'UE19CS204' }
// Use connect method to connect to the server
MongoClient.connect(url, { useUnifiedTopology: true }, function(err, client) {
if (err) {
throw err;
}
client.db(dbName).collection(collectionName).find(filter).toArray(function(err, docs) {
if (err) {
throw err;
}
console.log(docs);
})
client.close();
});

Why is `func` not a function here in node.js?

I am learning mongodb, and in the book, there's a code
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://127.0.0.1:27017/testdb";
module.exports = function (func) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
else {
console.log("connected");
func(db);
db.close();
}
});
};
I run this code, but throw the error TypeError: func is not a function, I googled, but lots of codes like this, my mongodb version is 4.0, and node.js version is 9.10, any ideas?
Whatever func you are passing must be a function.
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://127.0.0.1:27017/testdb";
module.exports = function (func) { //func must be function, dont pass just a variable
MongoClient.connect(url, function(err, db) {
if (err) throw err;
else {
console.log("connected");
func(db);
db.close();
}
});
};

NodeJs And MongoDB

db.getCollection('Leave').find({},{_id:0 ,
Can_It_Be_carry_forwarded:1})
this is working perfectly in the MongoDb Client CMD But not in the Below Code
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/Chatbot_Project";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("Chatbot_Project");
dbo.collection('Leave').find({}, {Can_It_Be_carry_forwarded:1}).toArray(function(err, result) {
if (err)
throw err;
console.log(result);
db.close();
})
});
Your problem is the find method, you are missing the projection field.
If you want to retrive only the Can_It_Be_carry_forwarded field you need the following: {projection:{Can_It_Be_carry_forwarded:1, _id: 0}} as the second argument.
Solution from a similar question: https://stackoverflow.com/a/48294672/4120554
Documentation: http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find
Try this:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/Chatbot_Project";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("Chatbot_Project");
dbo.collection('Leave').find({},{projection:{_id: 0, Can_It_Be_carry_forwarded:1}}).toArray(function(err, result) {
if (err)
throw err;
console.log(result);
db.close();
})
});

Wait until MongoDB has completed its query without using callbacks

I have an issue with the asynchrous programming model in Node.js.
I know this has been asked before, but so far I haven't found anything that I can use to solve my issue.
How do I force console.log from running before dbQueryName has been invoked and username has gotten its value from it?
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
function dbQueryName() {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("users").findOne({}, function(err, result) {
if (err) throw err;
return result.name
db.close();
});
});
}
var username = dbQueryName();
// Wait until dbQueryName() has been invoked and username has gotten its value before running this
console.log(username);
I have managed to get it working using a callback parameter in the function, but is there a way to do this with less code and indentation?
function dbQueryName(callback) {
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo.collection("users").findOne({}, function(err, result) {
if (err) throw err;
callback(result.name)
db.close();
});
});
}
dbQuery(function(result){
var username = result
console.log(username);
});
Yes if you are not passing a callback to MongoClient.connect or collection.findOne, it returns a promise.
But you need to add another function to use async await
init()
async function dbQueryName() {
const db = await MongoClient.connect(url);
const dbo = db.db("mydb");
const user = await dbo.collection("users").findOne({})
return user.name
}
async function init() {
try {
const name = await dbQueryName()
} catch (error) {
console.log(error);
}
}

node.js error when trying to insert data in mongodb collection

here is the code im trying to run in linux.
I've tried changing my bind ip to 0.0.0.0 and to 127.0.0.1 and even commented it but was no help. The error it displays is in screenshot here.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = { name: "Company Inc", address: "Highway 37" };
db.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 record inserted");
db.close();
});
});
error details
Collection#insertOne() requires mongodb#2, so if possible, upgrade your driver.
If not, you can use Collection#insert().

Resources