简体   繁体   English

Vue.js 和 Firestore 只更新不等于空字符串的变量

[英]Vue.js and Firestore update only the variables that are not equal to empty string

update only the variables that are not equal to empty string只更新不等于空字符串的变量

    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
      })

I only need to update the variables that will change.我只需要更新会改变的变量。 Firestore still changes them with an empty string.Thanks for help. Firestore 仍然使用空字符串更改它们。感谢您的帮助。

When using Firestore update() method, you don't need to pass all the values of your document.使用 Firestore update()方法时,您不需要传递文档的所有值。 Just passing the new changed values will be enough as the update() method will change just this field in your Firestore document.只需传递新的更改值就足够了,因为update()方法将仅更改 Firestore 文档中的此字段。 so you need to check if your fields have values from your app and pass them to the update() method accordingly.所以你需要检查你的字段是否有来自你的应用程序的值,并相应地将它们传递给update()方法。 Read more about this here 在此处阅读更多相关信息

You an can build an object the contains only fields to update, and pass that to update() .您可以构建一个仅包含要更新的字段的对象,并将其传递给update() You determine which properties to set on that object.您确定要在该对象上设置哪些属性。

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);

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

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