2017-07-11 27 views
3

我在云9做小马队的Web开发过程中尝试运行这段代码:猫鼬错误:'open()的`在猫鼬> = 4.11.0过时,

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) 

我试着用猫鼬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) 

你能帮忙吗?多谢你们!!!

+0

错误清楚地说--mpromise(猫鼬的默认亲mise库)已被弃用,请插入自己的承诺库:http://mongoosejs.com/docs/promises.html 您可以使用蓝鸟或任何其他您感到满意的承诺库。希望这可以帮助! –

+2

_“无法连接到服务器[localhost:27017]”_。你真的有一个MongoDB服务器运行? – robertklep

+1

'不建议使用'警告是来自猫鼬4.11的问题。你的问题看起来像你的本地mongdb还没有开始。尝试在终端/ cmd中输入'mongo'来检查。 – Khay

回答

1

这里是我用来解决这个问题,如果你检查猫鼬DOC:

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:真上面的配置),

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

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