question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle query params

See original GitHub issue

I’m pretty sure this is a bug in pathToRegExp (why is this project not using path-to-regexp like many other path parsers?). But if I provide the following URL: http://localhost:8989/api/book?query=:query this gets parsed to the following regex: http:\/\/localhost:8989\/api\/book?query=(?<query>.+?(?=\/|$))\/?

You may notice that the ? for the search string is not escaped. I believe that to be a bug in the parser. If I escape it myself, then things work just fine.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Mar 23, 2020

Yup, this works:

  rest.get(`${apiUrl}/books`, async (req, res, ctx) => {
    if (!req.query.has('query')) {
      return ctx.fetch(req)
    }
    const query = req.query.get('query')

    let matchingBooks
    if (query) {
      matchingBooks = matchSorter(allBooks, query, {
        keys: [
          'title',
          'author',
          'publisher',
          {threshold: matchSorter.rankings.CONTAINS, key: 'synopsis'},
        ],
      })
    } else {
      // return a random assortment of 10 books not already in the user's list
      matchingBooks = getBooksNotInUsersList(getUser(req).id).slice(0, 10)
    }

    return res(ctx.json({books: matchingBooks}))
  }),

Thanks!

1reaction
kentcdoddscommented, Mar 21, 2020

I tried the above suggestion and that didn’t work for me. I don’t have time to investigate why (will try later) but I thought I’d let you know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query Parameters - Branch.io
Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are...
Read more >
Query Parameters - Routing - Ember Guides
Query params allow for additional application state to be serialized into the URL that can't otherwise fit into the path of the URL...
Read more >
How can I get query string values in JavaScript?
You read/set parameters through the get(KEY) , set(KEY, VALUE) , append(KEY, VALUE) API. You can also iterate over all values for (let p...
Read more >
Query Parameters - FastAPI
As query parameters are not a fixed part of a path, they can be optional and can have default values. In the example...
Read more >
URLSearchParams - Web APIs - MDN Web Docs
Chrome Edge URLSearchParams Full support. Chrome49. Toggle history Full support. Edge... @@iterator Full support. Chrome49. Toggle history Full support. Edge... URLSearchParams() constructor Full support. Chrome49. Toggle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found