2

I am doing my where clause outside the findAll() method to allow the user to send filter's or not in the request body. One of the values on my request body is categoryId that can be sent or not.

This is my code :

     const where = {}
        if (categoryId) {
            where = { '$subcategories.category_id$': categoryId
         }
        } 

    try {
            const establishments = await establishment.findAll({
                attributes: [
                    "id",
                    "description",
                    "latitude",
                    "longitude",
                    [sequelize.literal(' (6371 * acos ( '
                        + 'cos( radians(' + latitude + ') ) '
                        + '* cos( radians( latitude ) ) '
                        + '* cos( radians( longitude ) - radians(' + longitude + ') )'
                        + '+ sin( radians(' + latitude + ') )'
                        + '* sin( radians( latitude ))))'), 'distance']
                ],
                include: [{
                    attributes: [],
                    model: subcategory, as: 'subcategories',
                    required: false,
                },
                {
                    attributes: ["id", "name"],
                    model: certificate, as: 'certificates',
                    required: false,
                },
                ],

                where: {
                    where
                },

                establishments: ['id'],
            });
 } catch (error) {
        console.log(error)
        res.status(400).json({ Error: "Error while fetching the establishment" });
    }

Error given :

UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable.
 UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
José Nobre
  • 4,407
  • 6
  • 20
  • 40
  • The error is actually pretty clear. You declare `where` as a constant and then try to assign it a value. – Aioros Apr 17 '20 at 19:41
  • Yes your right. – José Nobre Apr 17 '20 at 19:42
  • Also I seem to have an invalid value on the $subcategories.category_id$, am I doing something wrong? @Aioros – José Nobre Apr 17 '20 at 19:43
  • Does this answer your question? [Proper use of const for defining functions in JavaScript](https://stackoverflow.com/questions/33040703/proper-use-of-const-for-defining-functions-in-javascript) – Baruch Apr 17 '20 at 19:43
  • I am having this error on $subcategories.category_id$ saying invalid value, am I doing something wrong? – José Nobre Apr 17 '20 at 19:44

2 Answers2

3

You cannot assign values to const variables once it is declared.

either use let

let where = {}

or

const where = {}
if (categoryId) {
  where['subcategories.category_id'] = categoryId
}

change where: { where } to where: where

Akash Dathan
  • 4,348
  • 2
  • 24
  • 45
1
const where = {}

If you create a variable with const, then you can't redeclare it again. You should use let instead.

let where = {}
JW Geertsma
  • 857
  • 3
  • 13
  • 19
Rajesh Senapati
  • 201
  • 1
  • 8