Я учу себя Mongodb
. Сначала я запускал npm install --save mongoose uuid
в Terminal
чтобы начать работу. Цель моей программы - хранить пользователя в базе данных.
В Terminal
после запуска node index.js
я хочу сказать:
About to save!
Saved!
Но то, что я вижу в Terminal
(ниже):
Здесь index.js
var mongoose = require('mongoose');
var uuid = require('uuid');
var Schema = mongoose.Schema;
/* New code from suggested website in 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 = '[email protected]';
// 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!");
}
});
});
Ошибка в 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: '[email protected]',
__v: 0,
todos: [] }
Это не ошибка. В нем четко сказано, что это предупреждение: DeprecationWarning
. Также сообщение дает вам ссылку для разрешения предупреждения: http://mongoosejs.com/docs/connections.html#use-mongo-client