0

There are records in real-time DB that have a subarray: enter image description here

And I'm not sure how do I load it in my hook. I also created the type to receive it in an array. I found some examples, however, I'm looking for a way to do it easier.

type EventType = {
    id: string,
    autorID: string,
    autorNome: string,
    titulo: string,
    categoria: string,
    dateS: string,
    dateE: string,
    descricao: string,
    cancelado: string,
    confirmCount: number,
    confirmadosList: Record<string,{
        confirmedByUserID: string,
        confirmedByUserName: string,
        confirmedByUserAvatar: string
    }>
}

useEffect(()=>{
        const eventRef = database.ref(`eventos/${eventID}`)

        eventRef.once('value', evento =>{
            //console.log(evento.val());
            const eventValue = evento.val();

            const vari:EventType = {
                id: eventValue.key,
                autorID: eventValue.authorID,
                autorNome: eventValue.authorName,
                titulo: eventValue.title,
                categoria: eventValue.category,
                dateS: eventValue.startDate,
                dateE: eventValue.endDate,
                descricao: eventValue.description,
                cancelado: eventValue.canceled,
                confirmCount: eventValue.confirmados.length,
                confirmadosList: {
                    
                }
            }

            setEvento(vari)
            
            //setCountConfirm(Object.values(eventValue.confirmados ?? {}).length)
            //setHasConfirm(Object.values(eventValue.confirmados ?? {}).some(confirmado => confirmado.confirmedByUserID === user?.id))
        })
    },[eventID])

1 Answer 1

1

The confirmados node in your database is a Map, not a list. While you can get just the values of the keys under confirmados, that means you're throwing away the keys themselves.

If that is what you want, it'd be:

eventRef.once('value', snapshot =>{
  ...
  const confirmadosValues = [];
  snapshot.child('confirmados').forEach((confirmado) => {
    confirmadosValues.push(confirmadosValues.val());
  });
  ...

If you only want the value of confirmedByUserID, that inner line would be:

confirmadosValues.push(confirmadosValues.val().confirmedByUserID);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for sharing. I'm wondering to get both, the 'Eventos' node info and 'confirmados' node info.
Didn't the rest of your code work yet? Is eventValue not what you need for the rest of the data? If not, what value does it have?
The first part where I take the first node below of 'Evento' that does work, what is not working is the part that I try to take the 'confirmados' node. I'm wondering to take them in the same query (if not possible, then I have ideas about how to get it in another query, however, I would like to see if is there any possibility to take the data on the same query).
My code takes the confirmados from the read operation you already had. What didn't work about it when you tried my code?
Doesn't work on that way
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.