简体   繁体   English

Firestore - Web 版本 9 - 获取集合中其 ID 在 ID 数组中的所有文档

[英]Firestore - Web version 9 - get all documents in collection whose id is in array of ids

I'm new to Firestore and trying to form a query to return all documents whose doc.id is in an array of ids.我是 Firestore 的新手,正在尝试形成一个查询以返回其 doc.id 在 id 数组中的所有文档。

So something like this:所以像这样:

const getAlbumSongs = async (songUids) => {
    let albumSongs = [];
    const q = query(collection(db, 'songs'), where('id', 'in', songUids));
    const snap = await getDocs(q);
    if (snap) {
      for (const doc of snap) {
        albumSongs.push({
          songId: doc.id,
          albumId: doc.data().album_id,
          authorId: doc.data().author_id,
          songTitle: doc.data().song_title,
          filePath: doc.data().file_path,
        });
      }
    }
    return albumSongs;
  };

I should note I'm calling the getAlbumSongs() function from within a getAlbums() function, like this:我应该注意到我是从 getAlbums() function 中调用 getAlbumSongs() function,如下所示:

  async function getAlbums() {
    setIsLoadingAlbums(true);
    const snap = await getDocs(collection(db, 'albums'));
    setIsLoadingAlbums(false);
    let albumsData = [];
    if (snap) {
      snap.forEach(async (doc) => {
        albumsData.push({
          ablumId: doc.id,
          albumTitle: doc.data().album_title,
          albumCoverUrl: doc.data().cover_url || 'https://picsum.photos/300',
          albumSongs: await getAlbumSongs(doc.data().song_uids), // this might be problematic?
        });
      });
      console.log('albumsData', albumsData);
      return albumsData;
    }
  }

Your current code queries on a field called id in each document.您当前的代码查询每个文档中名为id的字段。 If you want the document ID itself, that is part of the metadata of the document and you'll want to query on documentId() for that:如果您想要文档ID 本身,那是文档元数据的一部分,您需要在documentId()上查询:

const q = query(collection(db, 'songs'), where(documentId(), 'in', songUids));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM