'Api call not recognising query parameter

I have here a backend router that fetches book data from a postgress database. I am trying to hook up a router that extracts and returns books with a particular genre. I am currently trying to pull the book data from the url and the console.log seems to return the appropriate word in the appropriate format. This api call works if:

  1. The genre is hardcoded
  2. The genre is in --> "'This Format'" <-- being single quotes inside double quotes exclusively.

{
"id": 1,
"title": "The Hunger Games",
"series": "The Hunger Games #1",
"author": "Suzanne Collins",
"description": "WINNING MEANS FAME AND FORTUNE.LOSING MEANS CERTAIN DEATH.THE HUNGER GAMES HAVE BEGUN. . . .In the ruins of a place once known as North America lies the nation of Panem, a shining Capitol surrounded by twelve outlying districts. The Capitol is harsh and cruel and keeps the districts in line by forcing them all to send one boy and once girl between the ages of twelve and eighteen to participate in the annual Hunger Games, a fight to the death on live TV.Sixteen-year-old Katniss Everdeen regards it as a death sentence when she steps forward to take her sister's place in the Games. But Katniss has been close to dead before—and survival, for her, is second nature. Without really meaning to, she becomes a contender. But if she is to win, she will have to start making choices that weight survival against humanity and life against love.",
"language": "English",
"isbn": "9780439023481",
"genres": [
"'Young Adult'",
"'Fiction'",
"'Dystopia'",
"'Fantasy'",
"'Science Fiction'",
"'Romance'",
"'Adventure'",
"'Teen'",
"'Post Apocalyptic'",
"'Action'"
],
"bookformat": "Hardcover",
"pages": 374,
"publisher": "Scholastic Press",
"coverimg": "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1586722975l/2767052.jpg",
"price": 509,
"bought": false,
"createdAt": null,
"updatedAt": null
},

const router = require("express").Router();
const {
  models: { Book },
} = require("../db");
//const { Op } = require('@sequelize/core');
const { Op } = require('Sequelize')
module.exports = router;

    router.get('/:genre', async(req, res, next) => {
        try{
            const genre = req.params.genre;
            const genreBook = await Book.findAll({
                where: {
                        genres:{
                            [Op.contains]: ["'Fiction'"]
                    }
                }
            });
            console.log('The genre url', typeof genre)
            console.log('stringified genre', typeof String(genre))
            res.json(genreBook)
        }catch(e){
            next(e)
        }
    })

Inserted here is one book in JSON format that is returned and the router. Please help



Solution 1:[1]

Two options here...

  1. Encode the quotes into the route parameter when you make the request, eg

    const genre = "'Fiction'"
    fetch(`https://example.com/${encodeURIComponent(genre)}`)
    

    This will make a request to https://example.com/'Fiction' which looks pretty terrible IMO

  2. Wrap the genre route param in quotes when you use it in your query

    Book.findAll({
      where: {
        [Op.contains]: [`'${genre}'`]
      }
    })
    

    Your request URL for this would look like https://example.com/Fiction

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phil