如何使用 React 从实时数据库加载子数组?
How do I load sub array from Realtime Database using React?
实时数据库中有记录有一个子数组:
而且我不确定如何将它加载到我的挂钩中。我还创建了在数组中接收它的类型。
我找到了一些示例,但是,我正在寻找一种更简单的方法。
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])
您数据库中的 confirmados
节点是 Map
,而不是列表。虽然您只能获得 confirmados
下键的值,但这意味着您将丢弃键本身。
如果这是你想要的,那就是:
eventRef.once('value', snapshot =>{
...
const confirmadosValues = [];
snapshot.child('confirmados').forEach((confirmado) => {
confirmadosValues.push(confirmadosValues.val());
});
...
如果您只想要 confirmedByUserID
的值,则内线为:
confirmadosValues.push(confirmadosValues.val().confirmedByUserID);
实时数据库中有记录有一个子数组:
而且我不确定如何将它加载到我的挂钩中。我还创建了在数组中接收它的类型。 我找到了一些示例,但是,我正在寻找一种更简单的方法。
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])
您数据库中的 confirmados
节点是 Map
,而不是列表。虽然您只能获得 confirmados
下键的值,但这意味着您将丢弃键本身。
如果这是你想要的,那就是:
eventRef.once('value', snapshot =>{
...
const confirmadosValues = [];
snapshot.child('confirmados').forEach((confirmado) => {
confirmadosValues.push(confirmadosValues.val());
});
...
如果您只想要 confirmedByUserID
的值,则内线为:
confirmadosValues.push(confirmadosValues.val().confirmedByUserID);