0

所以我试图通过 查询一个表userId,然后从我从第一个查询中获得的数组中查询另一个表。

DB 看起来像这样(一个用户可以有很多成员):

members:
id | userId | channelId | name | ...

channels:
id | ...

但问题是我的第一个查询返回一个我需要循环的数组。我将如何使用钩子在 React 中实现这一点?

const useChats = () => {
  const [chats, setChats] = useState()
  const client = useClient()

  const memberFilter = useFilter<DB.Member>((query) =>
    // hard coded for now
    query.eq('userId', '94a9aa95-c794-404a-9a37-7f424b215ebb').limit(10)
  )
  const [{ data: memberData }] = useRealtime<DB.Member>('members', {
    select: {
      columns: 'id, channelId, name, image, read',
      filter: memberFilter,
    },
  })

  const channelFilter = useFilter<DB.Channel>((query) =>
    // issue is here... how do i query `channel:id` for each member entry?
    // memberData is of type `DB.Member[] | null | undefined`
    query.eq('id', memberData[0].channelId).limit(10)
  )
  const [{ data: channelData }] = useRealtime<DB.Channel>('channels', {
    select: {
      columns: 'id, lastActive, lastMessage',
      filter: channelFilter,
    },
  })

  return chats
}

export { useChats }

类型:

export namespace DB {
  interface Member {
    id: string
    userId: string
    channelId: string
    name: string
    image: string
    read: boolean
  }

  interface Channel {
    id: string
    lastActive: string
    lastMessage?: string
  }
}

用于react-supabase查询

4

1 回答 1

0

您可以将 IN 运算符与映射函数一起使用,如下所示:

const channelFilter = useFilter<DB.Channel>((query) =>
  query.in('id', memberData.map(entry => entry.channelId)).limit(10)
)
于 2021-10-31T18:05:32.787 回答