我在教自己.get()
。起初,我在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 = 'test@test.com';
// 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
答案 0 :(得分:0)
这不是错误。它清楚地说这是一个警告:DeprecationWarning
。
此消息还为您提供了解决警告的链接:http://mongoosejs.com/docs/connections.html#use-mongo-client