微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – UnhandledPromiseRejectionWarning错误:主机标识符中的斜杠

我试图在我的终端中运行nodemon index.js但是我收到以下错误,我完全不知道这对我来说意味着什么是非常不清楚的.

可以请任何人向我解释如何解决这个问题?

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

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的连接.

精简版 :

您输入了错误登录网址:

mongoose.connect('mongodb://localhost:apiAuth');
                                      ^^^^^^^

我想你想写(或接近它的东西):

mongoose.connect('mongodb://localhost:'+apiAuth);

以下是MongoDB登录URL的示例:mongodb://127.0.0.1:27017 / my_db.
或者doc:Standard Connection String Format

长版:

问题的解决方法与上面相同,但您可以自己找到它.
就我而言,我继续这样做(因为我有完全相同的问题,有尽可能多的信息).

>隔离生成错误代码:您可以简单地评论代码的某些部分,以确定哪个区域崩溃.
>在与Mongoose连接后添加一个catch():mongoose.connect(…).catch((e)=> {console.log(e); throw e;}.这将直接表明相关的路线和一些其他信息.

这种技术在很多情况下都适用.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐