【问题标题】:because my return is [{"singles": [[Object], [Object]]}]因为我的回报是 [{"singles": [[Object], [Object]]}]
【发布时间】:2020-11-12 12:04:43
【问题描述】:

我正在使用 useEffect 从集合和子集合中获取信息,但是,从我的子集合返回的数据是 [{"singles": [[Object], [Object]]}]

useEffect(() => {
    fireStore
      .collection('users')
      // .where('category','==','band')
      // .where('status','==','approved')
      .where('email','==','marcelomenoli12@gmail.com')
      .get()
      .then( async (musics) => {
        const allSingles = musics.docs.map(async (single) => {
          const items = await single.ref.collection('musics').get();
          return {
            // cover: single.data().cover,
            // name: single.data().bandArtistName,
            singles: items.docs.map((item) => ({
              id: item.id,
              ...item.data(),
            }))
          }
        });
      const filteredSingles = await Promise.all(allSingles);
      setDATA(filteredSingles);
      });
  }, []);
  console.log(DATA);

【问题讨论】:

    标签: firebase react-native use-effect


    【解决方案1】:

    您应该使用多个 then 子句,因此每个异步操作都会返回值。否则内部等待响应不会准时到来。 我试图更新你的代码。我希望这会有所帮助。

    useEffect(() => {
        fireStore
          .collection('users')
          .where('email','==','marcelomenoli12@gmail.com')
          .get()
          .then( async (musics) => {
            const allSingles = musics.docs.map(async (single) => {
              return await single.ref.collection('musics').get();
              
          })
          .then((items)=>{
                // cover: single.data().cover,
                // name: single.data().bandArtistName,
                singles: items.docs.map((item) => ({
                  id: item.id,
                  ...item.data(),
                }))
          })
      }, []);
      console.log(DATA);
    

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2011-07-13
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多