在cli中启动nodemon index.js时会出现弃用警告

问题描述 投票:0回答:1

得到这样的错误

我正在做一个与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')
  });

config文件夹中的database.js

const crypto = require('crypto').randomBytes(256).toString('hex');


   module.exports= {
        uri: 'mongodb://localhost:27017/' + this.db,
            secret:
        'crypto',
            db:
        'login'
    }

怎么解决?

node.js command-line-interface mean-stack
1个回答
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);
    }
});

这应该工作,只需要一个额外的参数

© www.soinside.com 2019 - 2024. All rights reserved.