LoginSignup
0
0

Firestoreのin句でエラーを回避する

Posted at

はじめに

便利に使えるFirestoreなのですがちょっとはまったので書き残しておきたいと思い、書いていきます。

はまった事

公式より
Firestoreの検索でin句を使用しました。
Functionsの処理で組み込んだので、決まりきった値での検索ではなく、動的に検索条件を設定しました。

JavaScript
const list: string[] = []
(中略)
const collectionRef = db
    .collection('collection1')
    .where('id', 'in', list)
const collectionDoc = await collectionRef.get()

すると見事に最大30個の制限に引っかかってしまい、処理が中断してしまいました。

対応策

分割して処理を行うようにしたのですが、lodashライブラリの.chunk()メソッドを使用しました。

JavaScript
const list: string[] = []
(中略)
const chunks = lodash.chunk(list, 25) // 仮に25で分割
chunks.forEach(async (listMod) => {
    const collectionRef = db
        .collection('collection1')
        .where('id', 'in', listMod)
    const collectionDoc = await collectionRef.get()
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0