首页 > 解决方案 > How can I add a mysql Query to a node.js controller

问题描述

I have a node.js application and it works well if I use sequelize ORM as my Query to get data from a mysql database, however I have a mysql Query which I need to replace the current one with which cannot be converted to Sequelize ORM

This is the Controller file with the current orm query

// Include models
const db = require('../models')
const Shop = db.Shop

module.exports = {
  getHome: async (req, res) => {
    try {
      // retrieve all expense from record collection
      const records = await Shop.findAll({
        order: [['id', 'DESC']]

      })




      // check if any record is found
      const isEmptyRecord = records.length ? false : true
      // find total month


      res.render('index', { indexCSS: true, records,isEmptyRecord })
    } catch (err) {
      return console.log(err)
    }
  }
}

and this is the mysql query

"SELECT shops.*, (6371000 * acos(cos(radians(poi.lat )) \
  * cos(radians(shops.lat)) * cos(radians(shops.lon) - radians(poi.lon )) \
  + sin(radians(poi.lat )) * sin(radians(shops.lat)))) AS distance FROM shops \
  CROSS JOIN poi WHERE   poi.name like '%"+req.query.poi+"%' AND (6371000 \
    * acos(cos(radians(poi.lat)) * cos(radians(shops.lat)) \
    * cos(radians(shops.lon) - radians(poi.lon )) + sin(radians(poi.lat)) \
    * sin(radians(shops.lat)))) < "+req.query.radius+" AND shops.type like \
    '%"+shoptype+"%'AND shops.name like '%"+shopname+"%'";

How can I add that Query into the above file such that it can be executed and I see the result in the webpage.

Thanks in advance

标签: mysqlnode.jsormsequelize.js

解决方案


推荐阅读