我在云端9上做colts web开发者课程试图运行此代码:
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)
你能帮忙吗?谢谢伙计!!!
答案 0 :(得分:2)
这是我用来解决这个问题的方法,如果你检查mongoose doc:
http://mongoosejs.com/docs/connections.html#use-mongo-client
<强> &GT;这种弃用是因为MongoDB驱动程序已弃用API 这对于支持MongoDB的mongoose连接逻辑至关重要 3.6,有关更多详细信息,请参阅此github问题和此博客文章。
使用useMongoClient,您可以在顶层声明这些选项,而不需要额外的嵌套。这是所有的清单 支持
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);
答案 1 :(得分:2)
我在npm remove mongoose
然后npm install mongoose@4.10.8 --save