我正在尝试将 firestore 集成到使用 expo 创建的 javascript 中的 react 本机应用程序中。我只为android制作它。使用 google 登录现在工作正常,但我无法使用 firestore 执行 CRUD 功能。这是我的 app.js 请指导我出了什么问题。当我尝试在我的 firestore 集合中添加条目时,出现标题中提到的错误
firebase 版本是 18.7.3 这里我不是为 ios 制作应用程序。请帮忙
import React, { useState, useEffect } from 'react';
import { Button, StyleSheet, Text, View, TextInput } from 'react-native';
import {
GoogleSignin,
GoogleSigninButton,
} from '@react-native-google-signin/google-signin';
import firebase from '@react-native-firebase/app';
import {firestore,collection,doc} from '@react-native-firebase/firestore';
const firebaseConfig = {
projectId: "myprojectid",
apiKey: 'theveryverylongapikeystring',
authDomain: 'myprojectid.firebaseapp.com',
databaseURL: 'myprojectid.firebaseio.com',
storageBucket: "myprojectid.appspot.com",
messagingSenderId: "senderidinformatofnumericalstring",
appId: '1:theappidstring',
measurementId: "G-themeasurementidstrind"
};
if (!firebase.apps.length) {
console.log("no firebase apps available");
firebase.initializeApp(firebaseConfig);
}
const db = firebase.firestore();
const addDataToFirestore = async () => {
try {
console.log("adddatatofirestore");
const docRef = await db.collection('users').add({
"field1": 'value1',
"field2": 'value2',
});
console.log('Document written with ID: ', docRef.id);
} catch (error) {
console.error('Error adding data: ', error);
}
};
const App = () => {
const [error, setError] = useState();
const [userInfo, setUserInfo] = useState();
const [name, setName] = useState('');
useEffect(() => {
GoogleSignin.configure({
webClientId:"thewebclientid.apps.googleusercontent.com",
});
}, []);
const signin = async () => {
try {
await GoogleSignin.hasPlayServices();
const user = await GoogleSignin.signIn();
setUserInfo(user);
setError();
} catch (e) {
setError(e);
}
};
const logout = () => {
setUserInfo();
GoogleSignin.revokeAccess();
GoogleSignin.signOut();
};
return (
<View style={styles.container}>
<Text>{JSON.stringify(error)}</Text>
{userInfo && <Text>{JSON.stringify(userInfo.user)}</Text>}
{userInfo ? (
<View>
<Button title="Logout" onPress={logout} />
</View>
) : (
<View>
<TextInput
style={styles.input}
value={name}
onChangeText={setName}
placeholder="Enter your name"
/>
<Button title="Add Data" onPress={addDataToFirestore} />
<GoogleSigninButton
size={GoogleSigninButton.Size.Standard}
color={GoogleSigninButton.Color.Dark}
onPress={signin}
/>
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 10,
padding: 10,
},
});
export default App;
以下是我的app.config.js
export default {
"expo": {
"name": "GirishBichayat",
"slug": "GirishBichayat",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"plugins": ["@react-native-google-signin/google-signin",
"@react-native-firebase/app",
"@react-native-firebase/perf",
"@react-native-firebase/crashlytics"
],
"assetBundlePatterns": [
"**/*"
],
"android": {
"googleServicesFile": "./google-services.json",
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
},
"package": "com.josprachi.gbkakl"
},
"ios": {
"bundleIdentifier": "com.josprachi.gbkakl"
},
"web": {
"favicon": "./assets/favicon.png"
},
"extra": {
"eas": {
"projectId": "f9cc1c04-c7ee-4c80-b335-ddc9a4389f0b"
}
}
}
}
以下是我的package.json
{
"name": "girishbichayat",
"version": "1.0.0",
"scripts": {
"start": "expo start --dev-client",
"android": "expo run:android",
"ios": "expo run:ios",
"web": "expo start --web"
},
"dependencies": {
"@react-native-firebase/app": "^18.7.3",
"@react-native-firebase/auth": "^18.7.3",
"@react-native-firebase/crashlytics": "^18.7.3",
"@react-native-firebase/firestore": "^18.7.3",
"@react-native-firebase/perf": "^18.7.3",
"@react-native-google-signin/google-signin": "^11.0.0",
"expo": "~49.0.15",
"expo-build-properties": "~0.8.3",
"expo-dev-client": "~2.4.12",
"expo-splash-screen": "~0.20.5",
"expo-status-bar": "~1.6.0",
"react": "18.2.0",
"react-native": "0.72.6"
},
"devDependencies": {
"@babel/core": "^7.20.0"
},
"private": true
}
这是我的app.config.js
export default {
"expo": {
"name": "GirishBichayat",
"slug": "GirishBichayat",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"plugins": ["@react-native-google-signin/google-signin",
"@react-native-firebase/app",
"@react-native-firebase/perf",
"@react-native-firebase/crashlytics"
],
"assetBundlePatterns": [
"**/*"
],
"android": {
"googleServicesFile": "./google-services.json",
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
},
"package": "com.josprachi.gbkakl"
},
"ios": {
"bundleIdentifier": "com.josprachi.gbkakl"
},
"web": {
"favicon": "./assets/favicon.png"
},
"extra": {
"eas": {
"projectId": "f9cc1c04-c7ee-4c80-b335-ddc9a4389f0b"
}
}
}
}