Home > Mobile >  Get Firebase Doc ID
Get Firebase Doc ID

Time:06-20

I'm trying to work with Firebase. For comfortable work I use "react-firebase-hooks".

How do I get the doc id and doc data at the same time to transfer them to props? So far I can only get the doc data.

const TodoList: FC = () => {
    const [user] = useAuthState(auth)
    const [todos] = useCollectionData(collection(db, `users/${user?.email}/todos`))

    return (
        <section className="todo-list">
            {todos?.map(todo => (
                <Todo isCompleted={todo.isCompleted} text={todo.text} />
            ))}
        </section>
    )
}

CodePudding user response:

You should be able to achieve this using custom converter

const yourConverter = {
    toFirestore(post) {
        return { ... }
    },
    fromFirestore(snapshot, options) {
        const data = snapshot.data(options)
        return {
            id: snapshot.id,
            data: data.text,
        }
    },
}
const reference = firestore
.collection(`users/${user?.email}/todos`)
.withConverter(converter);

Related helpful documentation can be found here

  • Related