Home > Blockchain >  So I'm trying to check if user is logged in before allowing a post to be submitted to my fireba
So I'm trying to check if user is logged in before allowing a post to be submitted to my fireba

Time:08-17

The Error it returns is

Warning: An unhandled error was caught from submitForm() [TypeError: t is not an Object. (evaluating '"_delegate" in t')]

and also

firebaseerror: invalid document reference. document references must have an even number of segments

My firebase version is 9.9.2

My code that the error is coming from I Traced the error to the uploadPostToFirebase function but i decided to drop the getUsername function too

    const userRef = collection(db, 'users')
    const [thumbnailUrl, setThumbnailUrl] = useState(PLACEHOLDER_IMG)
    const [currentLoggedInUser, setCurrentLoggedInUser] = useState(null)

    const getUsername = () => {
        const user = auth.currentUser
        const owner_uid = auth.currentUser.uid
        const unsubscribe = (userRef, where(
            owner_uid, '==', user.uid
            ), limit(1), onSnapshot((userRef),{
                next: (snapshot => snapshot.docs.map(doc => {
                            setCurrentLoggedInUser({
                                username: doc.data().username,
                                profilePicture: doc.data().profile_picture
                            })
                        }
                    )
                )
            })
            )
        return unsubscribe
    }

useEffect(() => {
    getUsername()
},[])


const uploadPostToFirebase = (imageUrl, caption) => {
    const unsubscribe = (doc(db, 'users', auth.currentUser.email), collection(db, 'posts'), addDoc({
        imageUrl: imageUrl,
        user: currentLoggedInUser.username,
        profile_picture: currentLoggedInUser.profilePicture,
        owner_uid: auth.currentUser.uid,
        caption: caption,
        createdAt: serverTimestamp(),
        likes: 0,
        likes_by_users: [],
        comments: []
    }).then(() => navigation.goBack())
    )

    return unsubscribe
}```

CodePudding user response:

try this:

const uploadPostToFirebase = (imageUrl, caption) => {

 addDoc(collection(db, "posts"),{
        imageUrl: imageUrl,
        user: currentLoggedInUser.username,
        profile_picture: currentLoggedInUser.profilePicture,
        owner_uid: auth.currentUser.uid,
        caption: caption,
        createdAt: serverTimestamp(),
        likes: 0,
        likes_by_users: [],
        comments: []
        }).then(() => navigation.goBack())
    
      
}

CodePudding user response:

So the answer was to target the db collection at once

instead of const unsubscribe = (doc(db, 'users', auth.currentUser.email), collection(db, 'posts'), addDoc({

I used const unsubscribe = addDoc(collection(db,"users", auth.currentUser.email, "posts"),{

And it worked Thanks @navas

  • Related