简体   繁体   中英

So I'm trying to check if user is logged in before allowing a post to be submitted to my firebase firestore

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
}```

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())
    
      
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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