Value for argument "dataOrField" is not a valid Firestore value. Cannot use "undefined" as a Firestore value

公開日時

Firestoreのドキュメントを更新する際に↓のようにundefinedなfiledを含むオブジェクトを渡したところ、

await firestore.collection(`customers`).doc(uid).update({
  someField: undefined,
})

以下のエラーが発生した。

Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition.
Value for argument "dataOrField" is not a valid Firestore value.
Cannot use "undefined" as a Firestore value (found in field "someField").
If you want to ignore undefined values, enable `ignoreUndefinedProperties`.

undefinedはFirestoreの値には使えないが、オプションでundefinedを無視することもできるとのこと。

ということで、 firestore.settingsignoreUndefinedProperties を有効にした。

const firestore = admin.firestore()
firestore.settings({ ignoreUndefinedProperties: true })

これでundefinedなfiledは無視されてドキュメントが更新できるようになった。

参考


Related #firebase

Firestore エミュレータ使用時にデータをリセットする

エミュレータの場合のみ一括削除用のAPIが用意されている

Error: Input required and not supplied: firebaseServiceAccount

Dependabotによって作成されたPull Requestのみで発生している

Firebase公式Github ActionsでPull RequestごとにHostingのプレビュー環境を作る

手軽にプレビュー環境で動作確認できるようになったので、継続的なアップデートがやりやすくなった