我正在尝试一些方法来完全删除插入数据时为空的字段1
例如:我是 1 用户 A 我有姓名、年龄和国家/地区为 null 但我不希望它在数据库中添加国家/地区字段
像这样
db.collection("user").doc().set({
name: "A",
age: "18",
country: "" or country: null or country:'null'
}).then(() => {
console.log("Document successfully written!");
})
我想要这样
Collection(user)=>randomUid=>data(name:'A',age:'18')
最佳答案
您可以使用 spread syntax 实现此目的和一个条件对象。
const country = /* ... value from form/page/etc ... */;
db.collection("user").doc()
.set({
name: "A",
age: "18",
// if country is truthy and also not "null", include it, otherwise, omit it.
...(country && country !== "null" ? { country } : {})
})
.then(() => {
console.log("Document successfully written!");
})
.catch(err => console.error) // don't forget to handle errors!
关于javascript - 如何在 firestore 中插入和自动删除不必要的或空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66764310/