最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Firestore v9 best practices: ignoreUndefinedProperties vs. conditional addDocupdateDoc - Stack Overflow

programmeradmin0浏览0评论

Hey I just updated my project to use Firebase version 9 modular. Now after refactoring code I'm wondering which way is to get desired oute:
There is a document that has couple required fields (lets say: name, lastName). Now I want to add/update that document but it has multiple fields that are not required.

What are the best practices to achieve this? To conditionally add/update [1] or to use "ignoreUndefinedProperties" [2]?

[1] conditional add/update...

if(address){
  await updateDoc(
    doc(db, collection, id), {
      name,
      lastName,
      address,
    })
} else {
  await updateDoc(
    doc(db, collection, id), {
      name,
      lastName,
    })
}

[2] ignoreUndefinedProperties...

Also I couldn't find documentation about how to use "ignoreUndefinedProperties" in the new version so could someone guide me to right usage.

EDIT (Also is this (adding EDIT) best way to continue and fine down my question? I'm newbie so I'm looking also for StackOverflow best practices)

I'm testing required values but how should I do with values that are not required?
I have multiple collections and +20 calls to them and in many only 2-4 values are required while there might be 20 unrequired values.
This way I get the following error if any of those unrequired values are undefined and in updateDoc()...

Function updateDoc() called with invalid data. Unsupported field value: undefined (found in field exampleNotRequiredValue in document ...)

So for this case is it better to
[1] check if values are not undefined and in that case add them to update object
[2] use ignoreUndefinedProperties (as isn't this the point of this setting doesn't this do this check automatically and I don't need to change many functions?)

Hey I just updated my project to use Firebase version 9 modular. Now after refactoring code I'm wondering which way is to get desired oute:
There is a document that has couple required fields (lets say: name, lastName). Now I want to add/update that document but it has multiple fields that are not required.

What are the best practices to achieve this? To conditionally add/update [1] or to use "ignoreUndefinedProperties" [2]?

[1] conditional add/update...

if(address){
  await updateDoc(
    doc(db, collection, id), {
      name,
      lastName,
      address,
    })
} else {
  await updateDoc(
    doc(db, collection, id), {
      name,
      lastName,
    })
}

[2] ignoreUndefinedProperties...

Also I couldn't find documentation about how to use "ignoreUndefinedProperties" in the new version so could someone guide me to right usage.

EDIT (Also is this (adding EDIT) best way to continue and fine down my question? I'm newbie so I'm looking also for StackOverflow best practices)

I'm testing required values but how should I do with values that are not required?
I have multiple collections and +20 calls to them and in many only 2-4 values are required while there might be 20 unrequired values.
This way I get the following error if any of those unrequired values are undefined and in updateDoc()...

Function updateDoc() called with invalid data. Unsupported field value: undefined (found in field exampleNotRequiredValue in document ...)

So for this case is it better to
[1] check if values are not undefined and in that case add them to update object
[2] use ignoreUndefinedProperties (as isn't this the point of this setting doesn't this do this check automatically and I don't need to change many functions?)

Share Improve this question edited Sep 2, 2021 at 6:18 Hessuew asked Sep 1, 2021 at 13:27 HessuewHessuew 7831 gold badge9 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

"ignoreUndefinedProperties" may not be the best option. If name is required but is undefined for any reason, then it won't be written to the document. You can write security rules that'll check if name and nickname fields are present but it'll be best to conditionally add fields to your update object and make sure the required fields are present.

In case you want to use ignoreUndefinedProperties, you would have to use initializeFirestore instead of getFirestore:

import { initializeFirestore } from "firebase/firestore"

const db = initializeFirestore(app, {ignoreUndefinedProperties: true})

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论