UnhandledPromiseRejectionWarning Error: Slash in host identifier

本秂侑毒 提交于 2021-02-07 12:55:34

问题


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);

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;

Terminal

[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.

回答1:


The problem comes from your connection to MongoDB via Mongoose.


Short version :

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);

Here's an example of a MongoDB login URL : mongodb://127.0.0.1:27017/my_db. Or the doc: Standard Connection String Format


Long version :

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).

  1. Isolate the code that generates the error: You can simply comment on parts of your code to determine which zone is crashing.
  2. 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.




回答2:


I also have same error as like above (Error: Slash in host identifier). I resolved the issue. I am accessing mongooses as like below. My database password contains @ so here is the problem when our password having @ special character we need to pass with the help of encodeURIComponent. I passed the user name and password as like below its working fine for me.

Error:

   mongoose.connect('mongodb://xxx-xx:7a?VNXd@@sabfpV8=gRLwnNvC_8@196.89.12.168:27017/xxxxx',function(err,db){
        if(err){
         console.log(err);
       }else {
           console.log('connected to the Test db');
       }
     }); 

Resolved code:

 mongoose.connect('mongodb://xxx-xx:'+encodeURIComponent("7a?VNXd@#@safpV8=gRLwnNvC_8")+'@196.89.12.168:27017/xxxxx',function(err,db){
        if(err){
         console.log(err);
       }else {
           console.log('connected to the Test db');
       }
     }); 



回答3:


I was facing similar error, what i did is

Previous Error Causing Version:

mongoose.connect("mongodb://localhost:cat_app");

Working Version

mongoose.connect("mongodb://localhost/cat_app");

Both are same except : is replaced with /



来源:https://stackoverflow.com/questions/49682075/unhandledpromiserejectionwarning-error-slash-in-host-identifier

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