Node.js Sequelize-使用自定义where获取所有数据

Node.js Sequelize-使用自定义where获取所有数据,node.js,express,sequelize.js,Node.js,Express,Sequelize.js,如何使用自定义where条件获取数据?在这个问题上,, 我有一个类似的问题,但我相信这一个使用MySQL内置函数,它获取半径范围内的数据 我有好几种型号 房子 地址 任务 每个房子都有许多任务。每个房子都有一个地址 调用/gettask时,我需要获取所有任务,但有以下限制: 住宅地址直线距离必须为N公里,距离请求的lat long 我可以使用findAndCountAll轻松地完成这项工作,然后在将结果返回给客户端之前进行计算,但我确信这会降低运行速度/效率,或者会中断分页 以下是我目前掌握

如何使用自定义where条件获取数据?在这个问题上,, 我有一个类似的问题,但我相信这一个使用MySQL内置函数,它获取半径范围内的数据


我有好几种型号

  • 房子
  • 地址
  • 任务
  • 每个
    房子
    都有许多
    任务
    。每个
    房子都有一个
    地址

    调用
    /gettask
    时,我需要获取所有任务,但有以下限制:

    住宅地址直线距离必须为N公里,距离请求的lat long

    我可以使用
    findAndCountAll
    轻松地完成这项工作,然后在将结果返回给客户端之前进行计算,但我确信这会降低运行速度/效率,或者会中断分页


    以下是我目前掌握的情况:

    // Get all the available tasks.
    // Requirements:
    // 1. It accepts the coordinate from the client.
    // 2. The client's coordinate must be <= N-Kilometer straight distance.
    // 3. Return the tasks WITH PAYMENT and WITHOUT assigned USER.
    exports.getTasks = (req, res) => {
      const latitude = parseFloat(req.query.latitude)
      const longitude = parseFloat(req.query.longitude)
    
      if (!longitude || !latitude) {
        return res.status(200).send({
          errorCode: 101,
          message: "Error! Required parameters are: {longitude} and {latitude}."
        })
      }
    
      const page = myUtil.parser.tryParseInt(req.query.page, 0)
      const limit = myUtil.parser.tryParseInt(req.query.limit, 10)
    
      const houseLat = 32.9697
      const houseLong = -96.80322
    
      console.log("Computing distance of a house (" + latitude + ", " + longitude + ") --- to (" + houseLat + ", " + houseLong + ")")
    
      point1 = new GeoPoint(latitude, longitude)
      point2 = new GeoPoint(pLat, pLong)
    
      const distance = point1.distanceTo(point2, true)
    
      // Begin query...
      db.Task.findAndCountAll({
        where: null, // <----- don't know what to put.
        include: [
          {
            model: db.Order,
            as: "order"
          },
          {
            model: db.House,
            as: "house",
            include: [
              {
                model: db.Address,
                as: "address"
              }
            ]
          }
        ],
        offset: limit * page,
        limit: limit,
        order: [["id", "ASC"]],
      })
        .then(data => {
          res.json(myUtil.response.paging(data, page, limit))
        })
        .catch(err => {
          console.log("Error get all tasks: " + err.message)
          res.status(500).send({
            message: "An error has occured while retrieving data."
          })
        })
    }
    
    //获取所有可用任务。
    //要求:
    // 1. 它接受来自客户机的坐标。
    // 2. 客户的坐标必须是{
    const latitude=parseFloat(请求查询纬度)
    const longitude=parseFloat(请求查询经度)
    如果(!经度| |!纬度){
    返回资源状态(200)。发送({
    错误代码:101,
    消息:“错误!所需参数为:{longitude}和{latitude}。”
    })
    }
    const page=myUtil.parser.tryParseInt(req.query.page,0)
    const limit=myUtil.parser.tryParseInt(req.query.limit,10)
    常数houseLat=32.9697
    const houseLong=-96.80322
    log(“计算房屋(“+纬度+”,“+经度+”)到(“+houseLat+”,“+houseLong+”)的距离”)
    点1=新的地质点(纬度、经度)
    点2=新的地质点(平台、pLong)
    常量距离=点1。距离(点2,真)
    //开始查询。。。
    db.Task.findAndCountAll({
    
    其中:null,//我在这里发布我的解决方案。为了提供更多上下文,此rest应用程序类似于每个房屋的女佣/助手查找器。此特定端点(问题)是女佣/助手客户端应用程序获取需要清洁的可用房屋列表。条件是:

  • 房屋必须在距离客户端应用程序位置10公里的直线距离内
  • 该任务没有指定的清洁器


  • 因此,在这个解决方案中,我再次摆脱了分页——这有点复杂。在我看来,这种特定类型的应用程序设计(提供、工作等列表)不需要分页。然后直接计算直线距离。不需要谷歌API。不需要地理空间查询。我可能错了,但这正是我想要的-a只是澄清一下,你是否试图找到请求位置某个半径范围内的所有房屋?是的!但我真的更喜欢直线距离,而不是半径。明白吗l房屋,只有当房屋与用户的直线距离有点超出我的舒适区,因为你需要使用地理空间查询。再说一次,你在a点和B点之间的直线X-KM范围内寻找任何东西?我认为地理空间查询的事情与我上面贴的问题类似。但是正如我所说,这是没有必要的,是的,只是ptA和ptB之间的直线距离/直线。我问题中的代码已经可以计算直线距离,不需要花哨的Google API或其他东西。我只是想也许我可以把计算的东西挂到我的“where”上条件。或者这是不可能的?当然!顺便说一句,我错误地讨论了我试图得到“房子”而不是“任务”。
    // Get all the available tasks.
    // We are not using pagination here...
    // Makes our lives easier.
    // Requirements:
    // 1. It accepts the coordinate from the client.
    // 2. The client's coordinate must be <= N-Kilometer straight distance.
    // 3. Return the tasks WITH ORDER and WITHOUT USER.
    exports.getAvailableMissions = (req, res) => {
      const latitude = parseFloat(req.query.latitude)
      const longitude = parseFloat(req.query.longitude)
    
      if (!longitude || !latitude) {
        return res.status(200).send({
          errorCode: 101,
          message: "Error! Required parameters are: {longitude} and {latitude}."
        })
      }
    
      // Proceed with searching...
      // user id must equal to null. - means unassigned.
      // order must not be equal to null. - means paid.
      // the order condition is in the promise.
      const condition = { userId: { [op.is]: null } }
    
      // Begin query...
      db.Mission.findAndCountAll({
        where: condition,
        include: [
          {
            model: db.Order,
            as: "order"
          },
          {
            model: db.Asset,
            as: "asset"
          },
          {
            model: db.House,
            as: "house",
            include: [
              {
                model: db.Address,
                as: "address"
              }
            ]
          }
        ],
        limit: 10,
        order: [["id", "ASC"]],
      })
        .then(data => {
          let newData = JSON.parse(JSON.stringify(data))
          const tasks = newData.rows
          let newRows = []
    
          for (let task of tasks) {
            const house = task.house
            const address = house.address
            const houseLat = address.latitude
            const houseLong = address.longitude
    
            const point1 = new GeoPoint(latitude, longitude)
            const point2 = new GeoPoint(houseLat, houseLong)
    
            const distance = point1.distanceTo(point2, true)
            const distanceInMiles = distance * 0.621371
    
            console.log("Computing distance (" + latitude + ", " + longitude + ") --- to (" + houseLat + ", " + houseLong + ")")
            console.log("Miles: distance: ", distanceInMiles)
    
            // 10 miles max straight distance.
            const maxDistance = 10
    
            if (distanceInMiles <= maxDistance) {
              task.distanceFromMeInMiles = parseFloat(distanceInMiles)
              newRows.push(task)
            }
          }
    
          // Apply the new rows.
          delete newData.rows
          delete newData.count
          newData.total = newRows.length
          newData.data = newRows
    
          res.json(newData)
        })
        .catch(err => {
          console.log("Error get all tasks: " + err.message)
          res.status(500).send({
            message: "An error has occured while retrieving data."
          })
        })
    }