You need to enable JavaScript to run this app. 使用GeoFirestore查询附近位置时始终返回空对象{}的问题排查
优惠活动
大模型
产品
解决方案
定价
更多

使用GeoFirestore查询附近位置时始终返回空对象{}的问题排查

Firebase Functions 周边点位查询接口空返回问题

已编写两个云函数实现核心功能:

  • 支持用户完成位置签到操作
  • 基于给定坐标查询周边点位信息

签到功能实现(运行正常)

签到功能的代码逻辑如下:

.https.onRequest(async(request, response) =>{
     const uid = request.query.uid
     const cat = request.query.cat
     const addr = request.query.add
     const name = request.query.name
     const long = Number(request.query.long)
     const lat = Number(request.query.lat)
     const gpoint = new admin.firestore.GeoPoint(lat,long)
     const db = admin.firestore()
     const tstamp = Date.now()
     const adata = {
        createdate: tstamp,
        checkins: 1,
        lastcheckin: tstamp,
        status: null,
        createdby: uid,
        category: cat,
        address: addr,
        name: name,
        lat: lat,
        long: long,
        gid: null,
        g: {
          coordinates: gpoint,
        },
       }
      await db.collection('places').add(adata)
      response.send('OK')
  })

该功能运行无异常,存储的点位数据结构符合设计预期。

周边点位查询功能实现

基于GPS坐标查询附近点位的功能代码逻辑如下:

.https.onRequest(async(request, response) =>{
     const long= Number(request.query.long)
     const lat = Number(request.query.lat)
     const GeoFirestore = require('geofirestore').GeoFirestore
     const database = admin.firestore();
    const geofirestore = new GeoFirestore(database);
    const Geo = geofirestore.collection('places')
    const query = Geo.near({center: new admin.firestore.GeoPoint(lat,long), radius: 10 })
     query.get()
      .then((snapshot: { data: () => any }) =>{
       // const data =(snapshot.data())
        response.send(snapshot.data())
     })
       .catch((error: any) => {
           console.log(error)
           response.status(500).send(error)
       })
        })

异常表现

在浏览器中传入经纬度参数调用查询接口,访问路径为.../getplaces?long=46.93351240000001&lat=13.1321528时,接口始终返回空对象{},需定位该问题的根因。


内容的提问来源于stack exchange,提问作者Wyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.02 05:24:05