我将按照此视频创建React/Firestore聊天应用程序:https://youtu.be/k4mjF4sPITE
当我尝试在视频中创建“用户”集合时,集合没有生成。当我在云控制台中查看这些选项卡时,用户正在进行身份验证和存储,但没有作为集合存储在数据库中。以下是我从firebase.js中获得的代码:
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getStorage } from "firebase/storage";
import { getFirestore } from 'firebase/firestore'
const firebaseConfig = {
apiKey: 'removed',
authDomain: "removed",
projectId: "removed",
storageBucket: "removed",
messagingSenderId: "removed",
appId: "removed"
};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth()
export const storage = getStorage();
export const db = getFirestore();`
下面是相关部分的代码:
import React from 'react'
import { useState} from 'react'
import add from '../img/add.png'
import { createUserWithEmailAndPassword, updateProfile } from 'firebase/auth'
import { auth,storage, db } from '../firebase'
import { ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";
import { doc, setDoc } from "firebase/firestore";
import { useNavigate } from 'react-router-dom'
export const Register=() => {
const [err, setErr] = useState(false)
const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault();
const displayName = e.target[0].value;
const email = e.target[1].value;
const password = e.target[2].value;
const file = e.target[3].files[0];
console.log(displayName, email, password)
try{
//creates users
const res= await createUserWithEmailAndPassword(auth, email, password);
//create references so users can upload images
const storageRef = ref(storage, displayName);
const uploadTask = uploadBytesResumable(storageRef, file);
uploadTask.on((error) => {
setErr(true)
},
(error) => {
},
() => {
getDownloadURL(uploadTask.snapshot.ref).then( async(downloadURL) => {
await updateProfile(res.user, {
displayName,
photoURL: downloadURL,
})
await setDoc(doc(db, "users", res.user.uid), {
uid: res.user.uid,
displayName,
email,
photoURL: downloadURL,
});
我尝试过的解决方案:
- 从视频制作者的github复制/粘贴代码
- 更改cloude firestore规则的权限,因为一些视频评论建议
- 更改设置文档-〉添加文档,显示名称-〉
${displayName}
- 已查看此stackoverflow线程:Cloud firestore collection not showing up in cloud firestore's database
1条答案
按热度按时间92dk7w1h1#
确保在Firebase控制台中启动firestore,并且控制台中没有错误。