Uncaught Firebase error in Promise: Invalid document reference
P粉759457420
P粉759457420 2023-09-10 19:04:52
0
1
587

import { getDocs, collection, query, doc, addDoc } from "firebase/firestore/lite";
import { useState } from "react";
import { db } from "../firebaseConfig";
import { useEffect } from "react";


function EndGame(startGame){
    const {startGameHandler} = startGame;
    const startGameClick = startGameHandler[0];
    const time = startGameHandler[1];
    const [leaderboard, setLeaderboard] = useState([]);    
    const [user, setUser] = useState("");
    const [username, setUsername] = useState("")
    const [isAnonymous, setIsAnonymous] = useState(false);
   
   const loginAnonymously = () =>{
    console.log("login hivas ", user)
    setUser(username)
    setIsAnonymous(true)
    }
   const setScore= async(timeprop, userprop)=>{
        console.log(time, user)
        await addDoc(doc(db, "Leaderboard"), {
        name: userprop,
        time: timeprop,
      })
    }
        async function getLeaderboard(){
            const q = query(collection(db, "Leaderboard"));
            const chacSnapShot =  await getDocs(q);
            const char = chacSnapShot.docs.map(doc => doc.data());
            setLeaderboard(char)
        }
       
   useEffect(()=>{
    setScore(time, user)
    getLeaderboard()
   }, [isAnonymous])
    
    return(
        <div className={`endgame-page`}>

               {!isAnonymous && (
                <div className="endgame-div">
                    <input
                        type="text"
                        placeholder="Enter a username"
                        value={username}
                        onChange={e => setUsername(e.target.value)}
                    />
                    <button onClick={loginAnonymously}>Login Anonymously</button>
                </div>
            )}

            {isAnonymous && (
            <div className="endgame-div">
            <h1 className="endgame-heading">Leaderboard</h1>
            <div className="endgame-leaderboard">
            {leaderboard.map((data)=>{
                return(
                    <div key={data.name} className="user-container">
                        <p className="username">{data.name}</p>
                        <p className="userdata">{data.time}</p>
                    </div>
                )
            })}
            </div>
            <button className="endgame-button" onClick={startGameClick} >Start Game</button>
            </div>
            )}

        </div>
    )
}

export default EndGame

So I have this endgame component and when it renders, for some reason the setScore function is called and I think that's why I'm getting the following error:

Uncaught (in promise) FirebaseError: Invalid document reference. Document references must have an even number of segments, but leaderboards have 1.

is on line 27. Am I wrong in thinking it's because setScore is called when rendering? If not, what is the problem/solution?

In firebase I have a Leaderboard collection and I want to create documents from the user's time and name (there should be 1 document per user)

P粉759457420
P粉759457420

reply all(1)
P粉685757239

Methods addDoc Collection references should be used instead of document references. Document references are only used when you want to specify a document name, in which case setDoc should be used, please refer to the following sample code:

const setScore= async(timeprop, userprop)=>{
        console.log(time, user)
        await setDoc(doc(db, "Leaderboard", "<document-name>"), {
        name: userprop,
        time: timeprop,
      })
    }

To correct this problem, please see the sample code below:

const setScore= async(timeprop, userprop)=>{
        console.log(time, user)
        await addDoc(collection(db, "Leaderboard"), {
        name: userprop,
        time: timeprop,
      })
    }

You can view this documentation for more information.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template