简体   繁体   English

Firebase v9 不添加文档

[英]Firebase v9 not adding docs

I'm working with Firebase v9.我正在使用 Firebase v9。 The authentication works fine, but the Firestore does not work me for some reason.身份验证工作正常,但出于某种原因,Firestore 对我不起作用。 I don't even get an error--it just doesn't do anything.我什至没有收到错误——它什么也没做。

I tried addDocs() but still nothing works.我尝试addDocs()但仍然没有任何效果。

EDIT : actually, i was using the firebase @9.1.0 i upgraded it to @9.6.7 and it worked perfectly fine.编辑:实际上,我使用的是 firebase @9.1.0,我将其升级到 @9.6.7,并且运行良好。 i had to downgrade from @9.6.8 ( the latest ) to @9.1:0 because of the error ( Can't find variable: IDBIndex ) !由于错误(找不到变量:IDBIndex),我不得不从@9.6.8(最新)降级到@9.1:0!

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;

The function setDoc() asynchronously returns a promise which means all you're missing is the await keyword before you call the function. function setDoc()异步返回 promise,这意味着您在调用 function 之前只缺少await关键字。

const AddChat = async () => {
   const myDoc = doc(db, "Chats", input);
   const docData = {
     chatName: input,
   };

   await setDoc(myDoc, docData).then(() => {
     navigation.goBack();
   });
};

Edit: I think I see the real problem, It has to do with the v9 document reference.编辑:我想我看到了真正的问题,它与 v9 文档参考有关。 Try using collection() within the document reference.尝试在文档参考中使用collection()

const AddChat = async () => {
   const myDoc = doc(collection(db, "Chats"), input);
   const docData = {
     chatName: input,
   };

   await setDoc(myDoc, docData).then(() => {
     navigation.goBack();
   });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM