遇到这样的错误
我正在做一个与mongodb连接的简单节点js项目。如何解决此问题。
这是进入终端的错误
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
listening to serve 8080
Could not connect to database: { MongoNetworkError: failed to connect to server [localhost:27017] on first connect
mongo connect(index.js)
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const config = require('./config/database');
mongoose.Promise = global.Promise;
mongoose.connect(config.uri, (err)=>{
if(err){
console.log('Could not connect to database: ', err);
}
else{
console.log('Connected to database: ' +config.db);
}
});
app.get('/', (req, res)=>{
res.send('<h1>hello world</h1>');
});
app.listen(8080, ()=>{
console.log('listening to serve 8080')
});
配置文件夹中的database.js
const crypto = require('crypto').randomBytes(256).toString('hex');
module.exports= {
uri: 'mongodb://localhost:27017/' + this.db,
secret:
'crypto',
db:
'login'
}
如何解决?
答案 0 :(得分:1)
mongoose.connect(config.uri, {useNewUrlParser: true}, (err)=>{
if(err){
console.log('Could not connect to database: ', err);
}
else{
console.log('Connected to database: ' +config.db);
}
});
应该可以,只需要一个额外的参数