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