2017-07-07 91 views
1

我在教我自己Mongodb。起初,我跑npm install --save mongoose uuidTerminal开始的事情。我的程序的目标是将用户存储在数据库中。为什么在将用户输入数据库时​​出现错误?

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: [] } 

回答

0

这不是一个错误。它清楚地表明这是一个警告:DeprecationWarning。 此外,该消息为您提供了解决警告的链接:http://mongoosejs.com/docs/connections.html#use-mongo-client

+0

我在原始文章中编辑了我的代码。我在错误中添加了一些参考网站的新代码,但我仍然认为我没有正确使用它,因为它不起作用。 –

相关问题