Currently, I am delving into the world of Mongodb
. To kick things off, I initiated by executing npm install --save mongoose uuid
within the confines of Terminal
. The primary objective of my project revolves around storing user information in the database.
Post running node index.js
in Terminal
, my expectation is to witness:
About to save!
Saved!
Unfortunately, what greets me in Terminal
is (shown below):
This is the content of index.js
:
var mongoose = require('mongoose');
var uuid = require('uuid');
var Schema = mongoose.Schema;
/* Novel code from recommended source with an error */
var promise = mongoose.connect('mongodb://localhost:testMongo/testMongo', {
useMongoClient: true,
});
promise.then(function(db) {
db.model();
connection.openUri('mongodb://localhost:testMongo/testMongo', { /* options */ });
var userSchema = new Schema({
email: {
type: String,
unique: true
},
password: {type: String},
todos: [
{
text: {type: String}
}
]
});
userSchema.pre('save', function(next) {
console.log("About to save!");
var user = this;
user.password = uuid.v4();
next();
});
var User = mongoose.model('user', userSchema);
var email = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9febfaecebdfebfaecebb1fcf0f2">[email protected]</a>';
// var user = new User({
// email: email
// });
//
// user.save(function(err) {
// if(err) {
// return console.log(err);
// } else {
// return console.log("User was saved!");
// }
// })
//
// console.log("Outside of callback!");
var text = "This is a todo.";
User.findOne({email: email}, function(user, err) {
if(err) {
return console.log(err);
}
if(!user) {
return console.log("Couldn't find user!");
}
var count = user.todos.push({
text: text
});
console.log(count);
user.save(function(err){
if(err) {
console.log(err);
} else {
console.log("Saved!");
}
});
});
An error shown in Terminal
:
(node:14312) 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
{ _id: 595fe7c14a9810330c75aacc,
password: '297d5907-d9d7-49ef-800c-97a56aa395f7',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e591809691a591809691cb868a88">[email protected]</a>',
__v: 0,
todos: [] }