简体   繁体   English

如何确保 null 和空字符串字段未添加到我在 Firestore Reactjs 中的文档中

[英]How do I make sure null and empty string fields arent added to my documents in firestore Reactjs

I am adding objects to a document and I want to make sure empty strings and null instances don't add fields as I only want to create fields once they have been actually added我正在向文档中添加对象,并且我想确保空字符串和 null 实例不会添加字段,因为我只想在实际添加字段后创建字段

 await updateDoc(itemRef,
 {thing: '3232',
    thing2: null,
    thing3:'' });
    }

how do I make sure only thing: 3232 is in the new document and the best way to go about it?我如何确定唯一的事情:3232是在新文件中和go有关的最好方法吗?

Probably easiest to just write a function that filters the property keys based on value:可能最简单的方法是编写一个基于值过滤属性键的 function:

function filterInPlace(obj) {
    for (const [key, value] of Object.entries(obj))
        if (obj[key] == null || obj[key] == '')
            delete obj[key]
}

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

相关问题 如何检查以确保表单字段不是空字符串? - How do I check to make sure form fields are not empty strings? 如何将我的集合中的文档放入一个对象数组中,这些对象的字段来自 Google Firestore? - How do I put my Documents from my Collection into an array of objects with their own fields getting from Google Firestore? 如何确保回调仅添加一次? - How do I make sure a callback is only added once? 使用是的,我如何确保 2 个字段需要不同 - Using Yup how do i make sure that 2 fields need to be different 我如何确保该字段不只填充空白空间? - how do i make sure the field is not filled in with empty spaces only? 如何确保我的函数对我的变量有效? - How do I make sure my function works for my variable? 如何确保我的Ajax调用始终成功? - How do I make sure my ajax calls are always successful? 如何检索作为文档放置在 Firebase Cloud Firestore 中的 UID - How do I retrieve the UID's that I have placed as documents in my Firebase Cloud Firestore 在获取元素的边框宽度之前,如何确保添加了一个类? - How do I make sure a class is added before getting the border width of the element? 如何在创建用户时将其添加到“用户”Firestore 中? - How do I make it so when I create a user it gets added to a 'user' Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM