Skip to content

Instantly share code, notes, and snippets.

@AlejoJamC
Created July 4, 2017 06:50
Show Gist options
  • Save AlejoJamC/e1f847e6456464b2c4cfaeba0007e4fc to your computer and use it in GitHub Desktop.
Save AlejoJamC/e1f847e6456464b2c4cfaeba0007e4fc to your computer and use it in GitHub Desktop.
Mongoose Deprecation Warning: [email protected]
// After some time looking around 'mongoose issues' i found this link:
// https://github.com/Automattic/mongoose/issues/5399
// As you can see this message is a kind of bug, i tried the first possible answer to fix it
// Also create a test document, just to check if the connection and model works
//
// Stack:
// node: v7.10.0
// npm: 5.0.4
// nodemon: 1.11.0
// mongoose: 4.11.1
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/management', { useMongoClient: true });
var schema = mongoose.Schema;
var testSchema = new schema({
"userEmail": String,
"userPassword": String
});
var Test = mongoose.model('Test', testSchema);
var newtest = Test();
newtest.userEmail = '[email protected]';
newtest.userPassword = 'password';
newtest.save(function (err, test) {
if(err){
console.log('Error creating a new test content');
console.log(err);
}
// Success
console.log('New test document created:');
console.log(JSON.stringify(test));
});
console.log('Model created');
// Console response:
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Model created
(node:5334) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
New test document created:
{"__v":0,"userPassword":"password","userEmail":"[email protected]","_id":"595b339946e7df14d66c5ae4"}
// Inside mongodb i get this object
{
"_id" : ObjectId("595b339946e7df14d66c5ae4"),
"userPassword" : "password",
"userEmail" : "[email protected]",
"__v" : 0
}
// So it works
// Now we have this warning:
// DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
// I did the same search inside mongoose github page and i found this one:
// https://github.com/Automattic/mongoose/issues/4291
// As you can see is another "weird behaivor" of mongoose so you need to add this line to you code:
// mongoose.Promise = global.Promise;
//
// Stack:
// node: v7.10.0
// npm: 5.0.4
// nodemon: 1.11.0
// mongoose: 4.11.1
var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/management', { useMongoClient: true });
var schema = mongoose.Schema;
var testSchema = new schema({
"userEmail": String,
"userPassword": String
});
var Test = mongoose.model('Test', testSchema);
var newtest = Test();
newtest.userEmail = '[email protected]';
newtest.userPassword = 'password';
newtest.save(function (err, test) {
if(err){
console.log('Error creating a new test content');
console.log(err);
}
// Success
console.log('New test document created:');
console.log(JSON.stringify(test));
});
console.log('Model created');
// Console response:
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Model created
New test document created:
{"__v":0,"userPassword":"password","userEmail":"[email protected]","_id":"595b368d4d4f7f15b77f6a8a"}
// And as you can see no MORE WARNINGS!!!
// Did i think that is correct this answer?:
// NO, I didn't
// Did it works?
// Yes it did
// Will you use this 2 solutions?
// yes i will
// Initial version of the main file
//
// Stack:
// node: v7.10.0
// npm: 5.0.4
// nodemon: 1.11.0
// mongoose: 4.11.1
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/management');
var schema = mongoose.Schema;
var testSchema = new schema({
"userEmail": String,
"userPassword": String
});
var userLogin = mongoose.model('userLogin', testSchema);
console.log('Model created');
// run command: nodemon index.js -L
// Console response:
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js`
Model created
(node:5288) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
// Use this code for connection, you can avoid one warning of previous versiones and you will be notify that mongoose is connected.
//
// Stack:
// node: v7.10.0
// npm: 5.0.4
// nodemon: 1.11.0
// mongoose: 4.11.1
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/management', { useMongoClient: true });
console.log('Connecting to MongoDB server, database: management');
var con = mongoose.connection;
con.once('open', function () {
console.log('Connected to MongoDB successfully!');
});
// run command: nodemon index2.js -L
// Console response:
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index2.js`
Connecting to MongoDB server, database: management
Connected to MongoDB successfully!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment