Deprecated body-parser?

大憨熊 提交于 2021-02-10 16:21:54

问题


I am getting the error message deprecated body-parser i've looked up other methods concerning parsing the data but none seem to work. the below code what ive got

const express = require('express');

const app = express();

const Task = require('./api/models/ListModels.js'); //created model loading here

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

const mongoose = require('mongoose');

// mongoose instance connection url connection


mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb'); 

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

const routes = require('./api/routes/ListRoutes.js'); //importing route
routes(app); //register the route

app.listen(3000, () => {
    console.log('running on 3000')
    })

The error:

PS C:\Users\demar\Desktop\New folder\ListAPI> node app.js
(node:10424) 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.
running on 3000
(node:10424) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (C:\Users\demar\Desktop\New folder\ListAPI\node_modules\mongodb-core\lib\topologies\server.js:562:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (C:\Users\demar\Desktop\New folder\ListAPI\node_modules\mongodb-core\lib\connection\pool.js:316:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (C:\Users\demar\Desktop\New folder\ListAPI\node_modules\mongodb-core\lib\connection\connection.js:245:50)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
(node:10424) 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:10424) [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.

回答1:


It honestly tells you what to do..

To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

I added the options and it works perfectly..

Originally:

// Connect to Mongo:
mongoose.connect(db).then(() => console.log("Mongo DB Connected")).catch(err => console.log(err));

Current:

// Connect to Mongo:
mongoose.connect(db, { useNewUrlParser: true}).then(() => console.log("Mongo DB Connected")).catch(err => console.log(err));

This fixed the issue




回答2:


The error is that your version of Node's URL string parser, that Mongoose is using, is deprecated.

Pass in the option {useNewUrlParser: true} to mongoose.connect() to use the new Node Core URL API.

mongoose.connect('mongodb://localhost/Tododb', {useNewUrlParser: true})




回答3:


That error is appearing because you didn't specified the port number mongodb is running on.

mongoose.connect('mongodb://localhost:27017/databasename', { useNewUrlParser: true })

this will fix your issue :)



来源:https://stackoverflow.com/questions/51316865/deprecated-body-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!