我收到了不赞成使用的错误消息正文解析器,我在查找有关解析数据的其他方法,但似乎都没有用。下面的代码我得到了什么
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')
})
错误:
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.
答案 0 :(得分:1)
它诚实地告诉您该怎么做。
To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
我添加了选项,并且效果很好。
最初:
// Connect to Mongo:
mongoose.connect(db).then(() => console.log("Mongo DB Connected")).catch(err => console.log(err));
当前:
// Connect to Mongo:
mongoose.connect(db, { useNewUrlParser: true}).then(() => console.log("Mongo DB Connected")).catch(err => console.log(err));
此问题已解决
答案 1 :(得分:0)
错误是,Mongoose使用的Node URL字符串解析器的版本已过时。
将选项{useNewUrlParser: true}
传递给mongoose.connect()
,以使用新的Node Core URL
API。
mongoose.connect('mongodb://localhost/Tododb', {useNewUrlParser: true})
答案 2 :(得分:0)
出现此错误是因为您未指定运行mongodb的端口号。
mongoose.connect('mongodb://localhost:27017/databasename', { useNewUrlParser: true })
这将解决您的问题:)
答案 3 :(得分:0)
是的,body-parser 已被弃用。
不要再用了
从 Express 4.16+ 开始,正文解析功能已经内置到 express 中
你可以做到
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
直接来自express,无需安装body-parser。
因此您可以使用 npm uninstall body-parser
卸载 body-parser,只需使用上面的 express 代码即可。