Estoy trabajando con Firebase v9. La autenticación funciona bien, pero Firestore no me funciona por alguna razón. Ni siquiera recibo un error, simplemente no hace nada.
addDocs() pero aún nada funciona.
EDITAR : en realidad, estaba usando firebase @ 9.1.0, lo actualicé a @ 9.6.7 y funcionó perfectamente bien. ¡Tuve que bajar de @ 9.6.8 (el último) a @ 9.1.0 debido al error (No se puede encontrar la variable: IDBIndex)!
import React, { useLayoutEffect, useState } from "react"; import { Text, View, StyleSheet, TextInput, TouchableOpacity, KeyboardAvoidingView, Platform, ScrollView, Alert, } from "react-native"; import { AntDesign, Ionicons } from "@expo/vector-icons"; import { doc, setDoc } from "firebase/firestore"; import { db } from "../../firebase/firebaseConfig"; const NewChat = ({ navigation }) => { const [input, setInput] = useState(""); useLayoutEffect(() => { navigation.setOptions({ title: "Add a new Chat", headerBackTitle: "Chats", }); }, [navigation]); const AddChat = async () => { const myDoc = doc(db, "Chats", input); const docData = { chatName: input, }; setDoc(myDoc, docData).then(() => { navigation.goBack(); }); }; return ( <ScrollView> <View style={{ marginTop: 20, marginHorizontal: 20, borderColor: "black", borderWidth: 1, }} > <View style={styles.container}> <AntDesign name="wechat" size={40} color="black" style={{ alignSelf: "center" }} /> <TextInput placeholder="Enter a chat name" value={input} onChangeText={(text) => { setInput(text); }} style={{ flexGrow: 1, marginLeft: 20 }} /> <TouchableOpacity style={{ alignSelf: "center" }} onPress={AddChat}> <Ionicons name="checkmark-done-circle" size={40} color="black" /> </TouchableOpacity> </View> </View> </ScrollView> ); }; const styles = StyleSheet.create({ container: { flexDirection: "row", backgroundColor: "white", justifyContent: "center", height: 60, }, }); export default NewChat;La función setDoc() devuelve asincrónicamente una promesa, lo que significa que todo lo que te falta es la palabra clave await antes de llamar a la función.
const AddChat = async () => { const myDoc = doc(db, "Chats", input); const docData = { chatName: input, }; await setDoc(myDoc, docData).then(() => { navigation.goBack(); }); }; Editar: creo que veo el problema real, tiene que ver con la referencia del documento v9. Intente usar collection() dentro de la referencia del documento.
const AddChat = async () => { const myDoc = doc(collection(db, "Chats"), input); const docData = { chatName: input, }; await setDoc(myDoc, docData).then(() => { navigation.goBack(); }); };