这是要提交到数据库的对象的示例:
item = {
product_name: "3 Musketeers Truffle Crisp Bar",
brands: undefined,
ingredients_text: undefined,
nutrients: {
proteins_100g: 6.41,
fat_100g: 28.8
}
}
现在,由于该对象包含一些具有未定义值的键,我的应用程序中断了,并且收到错误消息,因为Firestore不支持未定义的数据类型。我不希望仅因为存在未定义的值而中断应用程序。我一直在寻找解决方案,发现可以使用
{ ignoreUndefinedProperties: true }
,但是当我使用npm安装firebase-admin时,出现此错误:======== WARNING! ========
firebase-admin appears to have been installed in an unsupported environment.
This package should only be used in server-side or backend Node.js environments, and should not be used in web browsers or other client-side environments.
但是我什至没有后端环境,我可以在Firebase网站上设置此规则吗?请有人帮我。
最佳答案
它与设置{ ignoreUndefinedProperties: true }
不同,但是firestore支持空值,因此您可以将其作为一种快速而肮脏的解决方法:
var someText = undefined;
item = {
product_name: "3 Musketeers Truffle Crisp Bar",
brands: undefined,
ingredients_text: someText || null,
nutrients: {
proteins_100g: 6.41,
fat_100g: 28.8
}
}
基本上,您可以为每个未定义的值添加|| null
。
关于javascript - 提交具有一些未定义值的对象时,如何使Firebase忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65555446/