Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnhandledPromiseRejectionWarning Error: Slash in host identifier

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.
like image 728
Markus Hayner Avatar asked Apr 05 '18 21:04

Markus Hayner


2 Answers

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.

like image 106
SiteXw Avatar answered Nov 16 '22 11:11

SiteXw


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@@[email protected]: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');
       }
     }); 
like image 2
Rajesh Kumar Kanumetta Avatar answered Nov 16 '22 12:11

Rajesh Kumar Kanumetta