Syntax error in MongoClient connection

kalex Source

Here is what my server is telling me:

MongoClient.connect('mongodb://<'yoda'>:<'yoda69'>@ds235778.mlab.com:35778/satr-wars-quotes', (err, client) => {
                    ^^^^^^^^^^^^^
SyntaxError: missing ) after argument list

Here is the code:

MongoClient.connect('mongodb://<'yoda'>:<'yoda69'>@ds235778.mlab.com:35778/satr-wars-quotes', (err, client) => {
  if (err) return console.log(err)
  db = client.db('satr-wars-quotes')
  app.listen(3000, () => {
    console.log('listening on 3000')
  })
})

I just don't see where this syntax error is being made.

javascriptnode.jsmongodbexpressmlab

Answers

answered 3 months ago scniro #1

You have single quotes in a singly-quoted string. I'd suggest escaping them, or double quote your string, with singles on the inside (or the inverse). Observe the following...

"mongodb://<'yoda'>:<'yoda69'>@ds235778.mlab.com:35778/satr-wars-quotes"

As far as if your connection string is correct, I am unsure, but this fixes your immediate syntax error.

answered 3 months ago tverghis #2

What are yoda and yoda69? Are they variable names, or literal strings?

If they are variable names, you need to either append them to the string using the + operator, or use ES6 template strings to interpolate their values into the larger string:

'mongodb://<' + yoda + '>:<' + yoda69 + '>@ds235778.mlab.com:35778/satr-wars-quotes'

or

`mongodb://<${yoda}>:<${yoda69}>@ds235778.mlab.com:35778/satr-wars-quotes`

If they are the literal strings, see scniro's answer.

comments powered by Disqus