You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore GeoPoint报undefined is not a constructor错误求助

React Native + Firestore 地理坐标写入报错排查

报错核心原因

运行时抛出undefined is not a constructor错误是两个问题叠加导致的:

  • 类名拼写错误:Firestore 地理坐标点的构造函数名是GeoPoint,你的代码里错写成了GeohPoint(多输入了一个h字符),对应的构造函数根本不存在
  • 引用路径错误:在@react-native-firebase系列SDK中,GeoPoint构造函数挂载在你单独导入的firestore模块对象上,不存在于firebase.firestore路径下,原写法取到的值本身就是undefined

修正后可运行代码

import firebase from '@react-native-firebase/app';
import firestore from '@react-native-firebase/firestore';
import * as geofirestore from 'geofirestore';
import storage from '@react-native-firebase/storage';

// 初始化Geofirestore,直接传入已导入的firestore实例即可
const GeoFirestore = geofirestore.initializeApp(firestore);

// 你提前定义的经纬度
const lat = 35.19580846863968;
const long = -3.911421988159418;

const geocollection = GeoFirestore.collection("mycollection");
await geocollection.add({
  title: title,
  timestamps: firestore.FieldValue.serverTimestamp(),
  price: price,
  // 修正构造函数引用与拼写,这里直接用你提前定义的经纬度变量,不要硬编码无关坐标
  coordinates: new firestore.GeoPoint(lat, long),
  climatisation: aclimatisation,
  images: firestore.FieldValue.arrayUnion(...downloadUrls),
});

额外注意事项

  • 原代码提前定义了目标经纬度变量,但写入时硬编码了其他地区的坐标,修正后已替换为你预定义的变量,避免存入错误位置
  • Geofirestore 会在你写入coordinates字段时自动生成geohash索引字段,不需要手动计算哈希值,后续做附近位置查询直接调用geocollection提供的查询API即可
  • FieldValue.arrayUnion是可变参数方法,如果你要批量存入数组里的所有图片地址,直接把数组展开传入即可,不需要再包裹一层对象

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 21:57:27