gpt4 book ai didi

javascript - 为什么在将用户输入数据库时​​出现错误?

转载 作者:行者123 更新时间:2023-11-28 04:26:49 25 4
gpt4 key购买 nike

我正在自学Mongodb。首先,我在 Terminal 中运行 npm install --save mongoose uuid 来开始操作。我的程序的目标是将用户存储在数据库中。

终端中,运行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!");
}
});
});

终端中出现错误:

(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: 'test@test.com',
__v: 0,
todos: [] }

最佳答案

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

关于javascript - 为什么在将用户输入数据库时​​出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com