简体   繁体   中英

Syntax error in MongoClient connection

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM