Seuqlize won't execute query that works fine on MySQL Workbench

Discussão em 'Angular' iniciado por Hamzah Alkhateeb, Outubro 20, 2024.

  1. I'm working on an Express application using Sequelize to query a MySQL database. I have a route that retrieves the closest restaurants based on user-provided latitude and longitude. While the SQL query works perfectly in MySQL Workbench, I encounter an "Internal Server Error" when executing the same query in my application.

    app.post('/getCloseRestaurants', async (req, res) => {
    const Ulong = parseFloat(req.body.long);
    const Ulat = parseFloat(req.body.lat);
    //lat: -34.782893, long: 138.628749
    console.log(`lat: ${req.body.lat}, long: ${req.body.long}`);


    const query = `
    SELECT
    id, name, location, lat, lon,
    (
    6371 * ACOS(
    COS(RADIANS:)Ulat)) * COS(RADIANS(lat)) *
    COS(RADIANS(lon) - RADIANS:)Ulong)) +
    SIN(RADIANS:)Ulat)) * SIN(RADIANS(lat))
    )
    ) AS DISTANCE
    FROM
    Restaurants
    ORDER BY
    DISTANCE ASC
    LIMIT 3;
    `;

    try {
    console.log("about to execute sql query!");
    const closestRestaurants = await sequelize.query(query, {
    replacements: {Ulat, Ulong},
    type: QueryTypes.SELECT
    });
    console.log("Executed successfully");
    console.log(closestRestaurants);

    if (closestRestaurants.length === 0){
    return res.json({success: false, message: "no restaurants available to display!"});
    }

    res.json({success: true, restaurantPackage: closestRestaurants});
    } catch (error){
    res.status(500).json({success: false, message: 'something went wrong'});
    }


    });


    I have attempted running the same query in MySQL workbench and it works fine and retrieves the intended data, but it breaks when it is run in the regular code. The problem appears exactly in the following section but I can't quiet pinpoint why:

    const closestRestaurants = await sequelize.query(query, {
    replacements: {Ulat, Ulong},
    type: QueryTypes.SELECT
    });

    Continue reading...

Compartilhe esta Página