DeprecationWarning:不建议使用当前URL字符串解析器,要使用新的解析器,请将选项{useNewUrlParser:true}传递给MongoClient.connect

时间:2019-04-23 19:29:47

标签: node.js mongodb mongoose

为什么仍然显示弃用警告,我已经添加了{useNewUrlParser:true}

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const errorController = require('./controllers/error');
const mongoConnect = require('./util/database').mongoConnect;
const User = require('./models/user');

const app = express();

app.set('view engine', 'ejs');
app.set('views', 'views');

const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use((req, res, next) => {
  User.findById('5baa2528563f16379fc8a610').then(user => {
  req.user = new User(user.name, user.email, user.cart, user._id);
  next(); 
})
.catch(err => console.log(err));
});

app.use('/admin', adminRoutes);

app.use(shopRoutes);

app.use(errorController.get404);

mongoConnect(() => {
                     app.listen(3000);
                  },{ useNewUrlParser: true });
    /*i added true to new url parser*/

错误:

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.
{ MongoNetworkError: connection 5 to cluster0-shard-00-01-ntrwp.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (D:\12-removing-deleted-items-from-the-cart\node_modules\mongodb-core\lib\connection\connection.js:352:9)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at _handle.close (net.js:561:12)
    at TCP.done [as _onclose] (_tls_wrap.js:360:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:9104) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 5 to cluster0-shard-00-01-ntrwp.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (D:\12-removing-deleted-items-from-the-cart\node_modules\mongodb-core\lib\connection\connection.js:352:9)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at _handle.close (net.js:561:12)
    at TCP.done [as _onclose] (_tls_wrap.js:360:7)
(node:9104) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9104) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

0 个答案:

没有答案