0

I am trying to find documents in my database using a query string:

app.get('/', (request, response) => {
  db.collection('mycollection').find(req.query).toArray((error, results) => {
    console.log(response)
  })
})

The request looks like this:

{ userName: 'K Moe' }

And the error I get is:

MongoError: query selector must be an object

I've tried changing req.query to req.query.userName and req.query['userName'] but that doesn't help...

K. Moe
  • 3
  • 1

1 Answers1

0

Try changing req.query from { userName: 'K Moe' } to { 'userName': 'k Moe' }

Greg Rebisz
  • 156
  • 7
  • The request is made using a query string url. How can I do that? – K. Moe Jul 08 '17 at 09:16
  • To use query params follow this link for nodejs + express: https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js Store the value of the username into a variable ParamVarHere `var ParamVarHere = req.params.username` When you get the value of your parameter, change the mongodb method to do the following: `db.collection('mycollection').find({'userName': ParamVarHere }).toArray((error, results) => { console.log(response) });` – Greg Rebisz Jul 10 '17 at 11:37
  • Please let me know if this is still an issue and I will write some sample code for you to download for express + mongo integration – Greg Rebisz Jul 10 '17 at 11:42