【发布时间】:2022-01-08 22:52:47
【问题描述】:
所以我正在做一个个人项目来学习 react-native 和 Firestore。
我有一个这样的数据库:
我希望我的代码在数组batteries 中添加一个新电池。
数组中的元素只是一个map{string, string}
问题是,当我用一个有效的新品牌更新数组时,如果我想再次用同一个品牌更新它, 所以到最后
batteries[0]: {'brand': 'cestmoi'}
batteries[1]: {'brand': 'cestmoi'}
数据库没有更新,没有任何错误。
我不明白为什么,我按照他们的教程进行操作。这是我的代码:
async function addData(collection, doc, value) {
console.log(`Add data ${value.brand}`)
try {
const result = await firestore()
.collection(collection)
.doc(doc)
.set({
batteries: firestore.FieldValue.arrayUnion(value)
})
console.log(result);
return result;
} catch (error) {
return error;
}
}
我习惯使用 try-catch,但我不知道 then...catch 是否更好。
【问题讨论】:
标签: javascript firebase react-native google-cloud-platform google-cloud-firestore