gpt4 book ai didi

mongodb - Mongoose 错误 : `open()` is deprecated in mongoose >= 4. 11.0,

转载 作者:可可西里 更新时间:2023-11-01 09:28:55 27 4
gpt4 key购买 nike

我在 cloud 9 上参加 colts web 开发人员类(class),尝试运行此代码:

var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/cat_app");


var catSchema = new mongoose.Schema({
name: String,
age: Number,
temperament: String
});

var Cat = mongoose.model('Cat', catSchema);
//add a new cat to db
var george = new Cat({
name: 'George',
age: 11,
temperament: 'Grouchy'
});

george.save(function(err, cat) {
if(err) {
console.log('Something went wrong!');
}else {
console.log('We just saved a cat to the DB: ');
console.log(cat);
}
});
//get all cats from DB and log each one

我不断收到这个错误:

`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
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http: //mongoosejs.com/docs/promises.html

events.js:141
throw er; // Unhandled 'error' event
^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:177:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1269:8)
at nextTickCallbackWith2Args (node.js:458:9)
at process._tickCallback (node.js:372:17)

我尝试使用 mongoose 4.10.8 但后来我得到了这个错误:

Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

events.js:141
throw er; // Unhandled 'error' event
^
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/home/ubuntu/workspace/database/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:177:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1269:8)
at nextTickCallbackWith2Args (node.js:458:9)
at process._tickCallback (node.js:372:17)

你能帮忙吗?谢谢大家!!!

最佳答案

这是我用来解决这个问题的方法,如果你查看 mongoose 文档:

http://mongoosejs.com/docs/connections.html#use-mongo-client

> This deprecation is because the MongoDB driver has deprecated an API that is critical to mongoose's connection logic to support MongoDB 3.6, see this github issue and this blog post for more details.

With useMongoClient you can instead declare these options at the top level, without all that extra nesting. Here's the list of all supported

options.mongoose.connect(myUri, {
socketTimeoutMS: 0,
keepAlive: true,
reconnectTries: 30
});

所以这对我有用(在上面的配置中添加 useMongoClient: true),

var mongodbUri = "mongodb://localhost:27017/mltdp";
var options = {
useMongoClient: true,
socketTimeoutMS: 0,
keepAlive: true,
reconnectTries: 30
};

var db = mongoose.connect(mongodbUri, options);

关于mongodb - Mongoose 错误 : `open()` is deprecated in mongoose >= 4. 11.0,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023793/

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