Javascript 无效位置:纬度必须是数字
我正在尝试使用执行地理查询。我想抓取给定集合中半径在1000范围内的所有文档 这是我的.js文件:Javascript 无效位置:纬度必须是数字,javascript,node.js,firebase,google-cloud-firestore,geofirestore,Javascript,Node.js,Firebase,Google Cloud Firestore,Geofirestore,我正在尝试使用执行地理查询。我想抓取给定集合中半径在1000范围内的所有文档 这是我的.js文件: const GeoFirestore = require('geofirestore').GeoFirestore; const GeoPoint = require('geopoint'); const firebase = require('firebase'); firebase.initializeApp({ projectId : 'my-project-id' }); mo
const GeoFirestore = require('geofirestore').GeoFirestore;
const GeoPoint = require('geopoint');
const firebase = require('firebase');
firebase.initializeApp({
projectId : 'my-project-id'
});
module.exports = {
getLocalDocuments: getLocalDocuments
}
async function getLocalDocuments() {
let latitude = 38.9537323;
let longitude = -77.3507578;
let radius = 1000;
// Create a Firestore reference
const firestore = firebase.firestore();
// Create a GeoFirestore reference
const geofirestore = new GeoFirestore(firestore);
// Create a GeoCollection reference
const geocollection = geofirestore.collection('myDocs');
const query = geocollection.near({
center: new GeoPoint(latitude, longitude),
radius
});
// Get query (as Promise)
await query.get().then((value) => {
console.log(`value.docs: ${value.docs}`); // All docs returned by GeoQuery
});
}
调用getLocalDocuments()函数时,我将获得以下堆栈跟踪:
Error: Invalid location: latitude must be a number
at validateLocation (/srv/node_modules/geofirestore/dist/index.cjs.js:567:15)
at validateQueryCriteria (/srv/node_modules/geofirestore/dist/index.cjs.js:600:9)
at GeoCollectionReference.GeoQuery.near (/srv/node_modules/geofirestore/dist/index.cjs.js:1416:9)
at getLocalDocuments (/srv/utils/locationService.js:45:33)
at Object.getLocalDocuments (/srv/utils/locationService.js:58:11)
at buildLocalTPRecipients (/srv/utils/notificationInstructorGenerator.js:370:43)
at notifyTPOfLocalJobsInstructionGenerator (/srv/utils/notificationInstructorGenerator.js:255:32)
at Object.generateNewJobInstructions (/srv/utils/notificationInstructorGenerator.js:98:29)
at handleOnCreate (/srv/db/jobs/onCreate.f.js:20:53)
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
以下是validateLocation方法:
函数验证位置(位置、标志){
如果(flag==void 0){flag=false;}
var误差;
如果(!位置){
错误='地质点必须存在';
}
else if(typeof location.latitude==‘未定义’){
错误='地理点上必须存在纬度';
}
else if(typeof location.longitude==‘未定义’){
错误='经度必须存在于地理点';
}
否则{
var纬度=位置纬度;
var经度=位置。经度;
if(纬度类型!='number'| | isNaN(纬度)){
错误='纬度必须是一个数字';
}
否则如果(纬度<-90 | |纬度>90){
错误='纬度必须在[-90,90]范围内';
}
else if(经度类型!='number'| | isNaN(经度)){
错误='经度必须是一个数字';
}
否则如果(经度<-180 | |经度>180){
错误='经度必须在[-180180]范围内';
}
}
如果(类型错误!==“未定义”&&!标志){
抛出新错误('无效位置:'+错误);
}
否则{
返回!错误;
}
}
知道为什么说纬度不是一个数字吗?我用isNaN()和其他方法检查过,它们都说这是一个数字 问题位于以下代码行的
validateLocation
功能中:
var latitude = location.latitude;
var longitude = location.longitude;
相反,这些行应该是:
var latitude = location.latitude();
var longitude = location.longitude();
因为纬度
和经度
是getter方法
如上所述
。纬度(inRadians)
:返回点的纬度。默认情况下,纬度以度为单位,除非inRadians为真
。经度(半径)
:返回点的经度。默认情况下,经度以度为单位,除非inRadians为true
复制步骤 创建某个
地质点
:
var GeoPoint = require('geopoint'),
statueOfLiberty = new GeoPoint(40.689604, -74.04455);
然后,console.log(自由状态)
,这是预期的输出:
GeoPoint {
_degLat: 40.689604,
_degLon: -74.04455,
_radLat: 0.7101675611326549,
_radLon: -1.2923211906575673
}
在此之后,为了验证上述地质点
,我使用了validateLocation(location,flag)
功能,该功能与您使用的功能相同,也可以找到:
GeoPoint {
_degLat: 40.689604,
_degLon: -74.04455,
_radLat: 0.7101675611326549,
_radLon: -1.2923211906575673
}
因此,您不应该使用名为“geopoint”的库。geopoint是firebase/firestore对象
const firebase = require('firebase');
const GeoPoint = firebase.firestore.GeoPoint;
做出这样的改变,其他的一切都会起作用。什么是
console.log(type of location.latitude)代码>说什么?(或经度)下面是validateLocation方法-您需要显示它是如何/在哪里的called@mwilson我无法将该语句添加到validateLocation方法,因为它是node modules包的一部分。部署cloud函数时,我将忽略此文件夹。@JaromandaX正在传递geocollection.near({…})调用中传递的对象的中心属性。希望这有帮助。什么值是location.latitude
和location.longitude
保持?您可能需要添加一些console.log语句进行调试,因为有些地方似乎有点不对劲。至于,您可以使用getLatitude()
和getLongitude()
来检索特定地质点对象的值。请记住,GeoPoint(双纬度,双经度)
是一个元组。
var latitude = location.latitude();
var longitude = location.longitude();
const firebase = require('firebase');
const GeoPoint = firebase.firestore.GeoPoint;