0

I want to search some rows from the database.

Phones.findAndCountAll({
        where: {
            id: (req.body.id) ?  req.body.id : { $ne: null },
            description: (req.body.description) ? { $like: '%' + req.body.description.trim() + '%' } : { $ne: null },
            price: {
                $and: {
                  {(req.body.st_price) ? {$gte: req.body.st_price}  : { $ne: null } },
                  {(req.body.en_price) ? {$lte: req.body.en_price}  : { $ne: null } },
                }
            }
        }
    })

But when I run this it doesnt work. I want to search if request.body.? exist. Should I use the between operator? How can I solve this problem?

1 Answer 1

1

I think , you should build your query first and then use it inside sequelize :

let price_conditions = {};
if(req.body.st_price){
    price_conditions['$gte'] = req.body.st_price;
}
if(req.body.en_price){
    price_conditions['$lte'] = req.body.st_price;
}
price_conditions['$ne'] = null;

Phones.findAndCountAll({
    where: {
        id: (req.body.id) ?  req.body.id : { $ne: null },
        description: (req.body.description) ? { $like: '%' + req.body.description.trim() + '%' } : { $ne: null },
        price : price_conditions
    }
})

For More Detail : DO READ

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.