这是我与mlab
连接的代码。但根本无法连接。出现错误:
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const {connection, Schema} = mongoose;
const databaseURL = 'mongodb://3gwebtrain:Chennaixxx@ds125683.mlab.com:25683/users';
var db = mongoose.createConnection(databaseURL);
db.on('error', console.error.bind(console, 'connection error:'));
const UserSchema = new Schema({
firstName:String,
lastName:String,
likes:[String]
});
const User = mongoose.model('User', UserSchema);
const addUser = (firstName,lastName) => new User({
firstName,
lastName
}).save();
const getUser = (id) => User.findById(id);
const removeUser = (id) => User.remove(id);
db.once('open', async() => {
try{
const newUser = await addUser('John', 'Smith')
} catch(error){
console.log(error);
} finally{
await connection.close();
}
})
错误:
connection error: { MongoNetworkError: failed to connect to server [ds125683.mlab.com:25683] on first connect [MongoNetworkError: connect ECONNREFUSED 54.194.35.191:25683]
at Pool.<anonymous> (D:\react\MERN\mern-cookbook\node_modules\mongodb-core\lib\topologies\server.js:503:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (D:\react\MERN\mern-cookbook\node_modules\mongodb-core\lib\connection\pool.js:326:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (D:\react\MERN\mern-cookbook\node_modules\mongodb-core\lib\connection\connection.js:245:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
message:
'failed to connect to server [ds125683.mlab.com:25683] on first connect [MongoNetworkError: connect ECONNREFUSED 54.194.35.191:25683]' }
该如何解决?