【发布时间】:2021-11-30 12:49:07
【问题描述】:
我是 React Native/Expo 的新手。我正在尝试将 Firebase Firestore 与我的应用集成,以便保留一些数据。
我在名为 config 的文件夹中创建了一个文件 firebase.js:
import * as firebase from "firebase";
import "firebase/firestore";
const firebaseConfig = {
// All the relevant firebase config data is here, removed for this post
};
// Initialize Firebase
if (!firebase.apps.length) {
const app = firebase.initializeApp(firebaseConfig).firestore();
} else {
const app = firebase.app().firestore();
}
在我的App.js 中,我正在尝试将设备的纬度和经度保存到 Firestore:
import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import firebase from "./config/firebase";
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const add = (x, y) => {
if (x === null || x === "" || y === null || y === "") {
return false;
}
firebase.collection("users")
.doc(1)
.set({
x: x,
y: y
});
};
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
add(location.coords.latitude, location.coords.longitude);
}
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{text}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
相当简单的东西。
但是,当我运行它时,我得到一个错误:
组件异常
_firebase.default.collection 不是函数。 (在 '_firebase.default.collection("users")` 中,'_firebase.default.collection' 未定义)
我做错了什么?
谢谢
【问题讨论】:
标签: firebase react-native google-cloud-firestore expo