-1

I'm working on express.js framework and sequlize.js orm.

I get the region and the type from the back-end.

Using those two variables I want to construct an object to pass to the sequlize orm.

        let region_id = req.swagger.params.region_id.value;
        let type = req.swagger.params.type.value;

        console.log("REGION", region_id) // will output 4,5,6,... etc
        console.log("TYPE", type) // will output is_lunch,is_dinner,... etc

That was my variables.

This is my object

const prepared_query = {
            where:{
            },
            attributes: ['price'],
            include: [{
                model: db.restaurants,
                attributes: ['region_id'],
                where: {
                    region_id: ''
                }
            }]
        }

        prepared_query.include[0].where.region_id = region_id;
        prepared_query.where[type] = true;
prepared_query.include[0].where.region_id = region_id;

successfully replaced the region_id as 4. but

prepared_query.where[type] = true;

is not gives me is_lunch:true' instead it gives me Cannot set property 'is_lunch' of undefined

HOW DO I ACHIEVE THIS USING JAVA SCRIPT?

margherita pizza
  • 6,623
  • 23
  • 84
  • 152

2 Answers2

0

you can do this.

prepared_query.include[0].where.region_id = region_id;
prepared_query[type] = true;

object.property = value;
{
    //static
    "property": value;
}
object[property] = value;
{
    //dynamic
    [property]: value;
}

read this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

seunggabi
  • 1,699
  • 12
  • 12
0

I didn't set anything to the prepared_query.where was the problem here.

    prepared_query.where = {};
    prepared_query.where[type] = true

Gives me the correct output.

margherita pizza
  • 6,623
  • 23
  • 84
  • 152