Here I have shop and user latitude and longitude and i want to show shop list in order by

54 views Asked by At

Here I have shop and user latitude and longitude i want to show my shop list between user and shop latitude and longitude . how i can do it in sequlize orm

 const latitude = req.query.latitude; // user latitude 
 const longitude = req.query.longitude; // user longitutde 

 const shopLists = await Shop.findAll({
            limit,offset,
            attributes:["latitude","longitude"],
            where:{
                category_id:category_id,
                status:1,
            },
            include :{
                model:Category,attributes:['id','name']
            }
        });

1

There are 1 answers

1
Wang Liang On
const limit;
// what is offset?
const shopLists = await Shop.findAll({
    where: {
        longitude: {
            [Op.ne]: null,
        },
        latitude: {
            [Op.ne]: null,
        },
        [Op.and]: [
            Sequelize.where(
                Sequelize.fn(
                    'ST_Distance',
                    Sequelize.fn('point', Sequelize.col('longitude'), Sequelize.col('latitude')),
                    Sequelize.fn('point', longitude, latitude),
                ),
                '<',
                limit,
            ),
        ],
    },
})