【问题标题】:Using GeoFire with Firebase Cloud Functions and TypeScript将 GeoFire 与 Firebase 云函数和 TypeScript 结合使用
【发布时间】:2019-12-23 20:07:46
【问题描述】:

我正在编写一个 Firebase 云函数来获取靠近某个地理位置 (similar question) 的位置。该函数接受两个参数:纬度和经度。

export const getDrinkingFountains = functions.https.onRequest((req, res) => {
  const latitude = req.query.latitude;
  const longitude = req.query.longitude;

  const ref = admin.database().ref("Fountains");
  const geoFire = new GeoFire(ref);
  // const keys: Fountain[] = {};

  return new Promise(function(resolve, reject) {
    const geoQuery = geoFire.query({
      center: [latitude, longitude],
      radius: 10.0
    });

    geoQuery.on("key_entered", function(key, location, distance) {
      // var fountain: Fountain = {key: key, latitude: location.latitude, longitude: location.longitude, distance: distance};
      // keys.push(fountain);
    });

    geoQuery.on("ready", function() {
      resolve(arrayShouldGoHere);
    });
  });
});

我的问题是它看起来像:

  • 我没有正确解析请求中的纬度和经度。事实上,GeoFire 抱怨Error: Invalid GeoFire location '40,40': latitude must be a number。使用 Postman,我实际上只是传递了两个没有字符串文字的整数。
  • 我不知道如何创建喷泉数组。我对 TypeScript 不太满意。

有什么提示吗?

【问题讨论】:

  • 您的纬度似乎以40,40 的形式出现,而不是数字。如果你记录latitudelongitude 的值是多少?
  • @C_Ogoo 通过打印出来我得到Latitude is 40 and longitude: 40,看起来是正确的。也许 TypeScript 会推断类型并将它们转换为字符串?
  • 那么此文档中的某个位置const ref = admin.database().ref('Fountains') 的值可能不正确
  • Typescript 仅在编译时.. 在运行时没有任何类型检查或推断
  • 检查typeof 的纬度和经度返回string。我不确定我应该如何“修复”它

标签: typescript firebase google-cloud-functions


【解决方案1】:

您可以使用Number 类将latitudelongitude 转换为数字

const latitude = Number(req.query.latitude);
const longitude = Number(req.query.longitude);    

【讨论】:

  • 谢谢,这行得通!现在,让我们修复这个数组。
猜你喜欢
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多