UnhandledPromiseRejectionWarning 错误:主机标识符中的斜线 [英] UnhandledPromiseRejectionWarning Error: Slash in host identifier
问题描述
我正在尝试在我的终端中运行 nodemon index.js,但我收到以下错误,我完全不知道这意味着什么,因为我很不清楚.
I am trying to run nodemon index.js in my terminal but I am getting the following error which I have absolutely no idea what it means as for me is very unclear.
谁能向我解释一下如何解决这个问题?
Can please anyone explain to me how to solve this?
index.js
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var app = express();
var router = require('./services/router');
mongoose.connect('mongodb://localhost:apiAuth');
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use('/v1', router);
var PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';
console.log('Listening on', HOST, PORT);
app.listen(PORT, HOST);
服务/router.js
services/router.js
var router = require('express').Router();
function protected(req, res, next) {
res.send('Here is the secret!');
}
router.route('/protected')
.get(protected);
module.exports = router;
终端
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Listening on 127.0.0.1 3000
(node:29104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Slash in host identifier
(node:29104) [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.
推荐答案
问题来自您通过 Mongoose 连接到 MongoDB.
The problem comes from your connection to MongoDB via Mongoose.
简短版本:
您输入了错误的登录网址:
You have entered a bad login URL:
mongoose.connect('mongodb://localhost:apiAuth');
^^^^^^^
我想你想写(或类似的东西):
I think you want to write (or something close to it):
mongoose.connect('mongodb://localhost:'+apiAuth);
以下是 MongoDB 登录 URL 的示例:mongodb://127.0.0.1:27017/my_db
.或文档:标准连接字符串格式
Here's an example of a MongoDB login URL : mongodb://127.0.0.1:27017/my_db
.
Or the doc: Standard Connection String Format
长版:
问题的解决方法与上述相同,但您可以自己定位.就我而言,我是这样做的(因为我遇到了完全相同的问题,而且信息量也差不多).
The resolution of the problem is the same as above, but you would have located it yourself. For my part, I proceeded like that (because I had exactly the same problem, with as much information).
- 隔离产生错误的代码:您可以简单地对代码的某些部分进行注释,以确定哪个区域崩溃了.
- 在与Mongoose的连接后添加一个
catch()
:mongoose.connect(...).catch((e) => { console.log(e); throwe; }
.这将直接指示相关行和一些附加信息.
- Isolate the code that generates the error: You can simply comment on parts of your code to determine which zone is crashing.
- Add a
catch()
after the connection with Mongoose:mongoose.connect(...).catch((e) => { console.log(e); throw e; }
. This will have indicated directly the line concerned and some additional information.
这种技术在很多情况下都有效.
This kind of technique works in a lot of cases.
这篇关于UnhandledPromiseRejectionWarning 错误:主机标识符中的斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!