我是 React Native/Expo 的新手。我正在嘗試將 Firebase Firestore 與我的應用程式集成,以便我可以保留一些資料。
我firebase.js在名為的檔案夾中創建了一個檔案config:
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' 未定義)
我究竟做錯了什么?
謝謝
uj5u.com熱心網友回復:
您沒有顯示您匯出的內容firestorefrom ./config/firebase,但它必須是您所期望的。
根據Firebase 檔案,您希望按照以下方式做一些事情:
var db = firebase.firestore()
db.collection("users").doc //etc
因此,在這種情況下,您希望db像firestore在您的config/firebase/
請注意,這是針對 Web API 的第 8 版,我假設您正在使用基于您的代碼檢查firebase.apps.length。如果您使用的是版本 9,則需要更新新 API 的代碼。
uj5u.com熱心網友回復:
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();
export default app;
} else {
const app = firebase.app().firestore();
export default app;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/311262.html
標籤:火力基地 反应原生 谷歌云firestore 世博会
