Skip to content Skip to sidebar Skip to footer

Using Where Condition In Sequelize For Filtering According To Distance

I have to filter the posts based on the distance from current location. Have attached the code below for reference,but it does not work (calculating distance works fine though),onl

Solution 1:

Maybe if you move your where distance condition out of the include condition inside the first mobile where. Also i suggest moving your attributes on top of the first where condition.

Hope these changes will fix your error or move you towards the solution.

const posts = await Post.findAll({
    attributes: 
        [[
            db.Sequelize.fn(
                "ST_Distance",
                db.Sequelize.col("location"),
                db.Sequelize.fn(
                    "ST_MakePoint",
                    parsedLocation.lat,
                    parsedLocation.lng
                )
            ),
            "distance"
        ]],
    where: {
       [Op.and]: {
            mobile: {
                [Op.ne]: mobile,
            },
            distance: {
                [Op.between]: [
                 0,100
                ],
            }}
    },
    include: {
        model: UserProfile,
        attributes: ["username"],
    },
});

Post a Comment for "Using Where Condition In Sequelize For Filtering According To Distance"