0

I tried to set dynamically change host and port in mongodb url but its not working. Can anybody give some suggestion? How to set host and port in mongodb url?

mongodb_url.js

host="localhost"
port="27017"
user ="root"
password = "mongodb"
let url = "mongodb://{user}:{password}@{localhost}:{port}";

url is not working how to set correctly using mongodb and node js

Ishwar Patil
  • 1,671
  • 3
  • 11
  • 19
Hari Smith
  • 143
  • 1
  • 9
  • You are trying to use [template literal strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals). Reference this for the correct syntax. – kennyvh May 30 '19 at 06:20

1 Answers1

1

you need to use template literals as per es6 try wrapping with ` (backtick) instead of " (quotes) like so:

let url = `mongodb://${user}:${password}@${host}:${port}`;

(note that it's 'host' not 'localhost' as per your definition)

Michael
  • 4,538
  • 5
  • 31
  • 58