0

Could somebody please point me in the right direction regarding the correct syntax of template literals ?

I have the following code (without template literals, works just fine) :

const dbUrl = 'localhost:27017/imgManager';
mongoose.connect('mongodb://' + dbUrl, { useNewUrlParser: true });

Now I'd like to put useNewUrlParser: true in a variable :

const dbUrl = 'localhost:27017/imgManager',
      dbOptions = 'useNewUrlParser: true';
mongoose.connect('mongodb://' + dbUrl, { dbOptions });

Doesn't work of course, mongoose.connect() complains it has no dbOptions :

$ node server.js 
Server up: http://localhost:3300
the options [dbOptions] is not supported

I guess template literals are the way to go, but what's the right syntax ? I tried the following, but none works :

`mongoose.connect('mongodb://' + dbUrl, { ${dbOptions} });`
mongoose.connect(`mongodb://${dbUrl}, { ${dbOptions} }`);
mongoose.connect(`mongodb:\/\/${dbUrl}, { ${dbOptions} }`);

Any idea ?

ChennyStar
  • 350
  • 1
  • 2
  • 11
  • 1
    Why do you want to have all the parameters as a *single string* and somehow expect the method to read that string and interpret what the parameters are? – VLAZ Mar 10 '19 at 12:57
  • 2
    The more I look at this, the more I think it's an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - I don't think your query has anything to do with template literals, nor is the solution to use them. If you want to store the `dbOptions` as a variable, you'd do `dbOptions = { useNewUrlParser: true }` and then call `mongoose.connect('mongodb://' + dbUrl, dbOptions);` – VLAZ Mar 10 '19 at 13:00
  • 2
    Template literals are used to create *text*, not *code*. –  Mar 10 '19 at 13:02

3 Answers3

1

Template literals don't work in arbitrary position, and they don't create arbitrary JS syntax. (Without a tag) they create strings only, and only a single value never two arguments at once. As options, you need to pass an object:

const dbUrl = 'localhost:27017/imgManager',
      dbOptions = { useNewUrlParser: true };
mongoose.connect(
   `mongodb://${ dbUrl }`, // first argument
    dbOptions // second argument
);

If you really get the options as a string, you would need to parse them into an object, e.g.:

const dbUrl = 'localhost:27017/imgManager',
      dbOptions = '"useNewUrlParser": true';
mongoose.connect(
   `mongodb://${ dbUrl }`, // first argument
    JSON.parse(`{${ dbOptions }}`) // second argument. The string is '{' + dbOptions + '}'
);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

Template literals are only for strings, not objects. dbOptions should be an object, not a string.

const dbOptions = { useNewUrlParser: true };

Then you use the variable itself:

mongoose.connect('mongodb://' + dbUrl, dbOptions);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

As dbOptions is a string in your approach, this { dbOptions } is creating a js object as follow:

// cause that mongoose is complaining because it's not expecting 
// options with property name dbOptions
{ dbOptions: 'useNewUrlParser: true' } 

You should do the following

const dbUrl = 'localhost:27017/imgManager',
      dbOptions = {useNewUrlParser: 'true'};    

// `mongodb://${dbUrl}` will generate the following string:
//  "mongodb://localhost:27017/imgManager" 
mongoose.connect(`mongodb://${dbUrl}`, dbOptions);
Ele
  • 33,468
  • 7
  • 37
  • 75