【问题标题】:Vue.js and Firestore update only the variables that are not equal to empty stringVue.js 和 Firestore 只更新不等于空字符串的变量
【发布时间】:2020-11-05 10:22:23
【问题描述】:

只更新不等于空字符串的变量

    const state = reactive({
      birthNumber: '',
      phoneNumber: '',
      DoctorName: '',
      DoctorPhone: '',
    })
      db.collection(state.user.uid).doc("Personal_Records").update({
        birthNumber: state.birthNumber,
        phoneNumber: state.phoneNumber,
        DoctorName: state.DoctorName,
        DoctorPhone: state.DoctorPhone
      })

我只需要更新会改变的变量。 Firestore 仍然使用空字符串更改它们。感谢您的帮助。

【问题讨论】:

    标签: javascript firebase vue.js object google-cloud-firestore


    【解决方案1】:

    使用 Firestore update() 方法时,您不需要传递文档的所有值。只需传递新更改的值就足够了,因为 update() 方法将仅更改 Firestore 文档中的此字段。所以你需要检查你的字段是否有来自你的应用程序的值,并相应地将它们传递给update() 方法。阅读更多关于这个here

    【讨论】:

      【解决方案2】:

      您可以构建一个只包含要更新的字段的对象,并将其传递给update()。您确定要在该对象上设置哪些属性。

      const update = {}
      if (shouldUpdateBirthNumber} {
          update.birthNumber = state.birthNumber
      }
      // repeat for each optional field to update
      
      db.collection(state.user.uid).doc("Personal_Records").update(update);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-19
        • 2021-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多