我正在开发一些与 Firestore 配合使用的云功能。我正在尝试获取特定文档的字段列表。例如,我有一个文档参考even.data.ref
,但我不确定该文档是否包含我正在查看的字段。我想获取字段名称的列表,但我不知道该怎么做。
我试图使用Object.keys()
方法来获取数据的键列表,但我只获取数字列表(0、1...),而不是字段名称。
我尝试使用documentSnapShot.contains()
方法,不过好像行不通。
exports.tryHasChild=functions.firestore.document('cities/{newCityId}')
.onWrite((event) =>{
if (event.data.exists) {
let myRef = event.data.ref;
myRef.get().then(docSnapShot => {
if (docSnapShot.contains('population')) {
console.log("The edited document has a field of population");
}
});
As the 有关将 Cloud Firestore 触发器用于 Cloud Functions 的文档 https://firebase.google.com/docs/functions/firestore-events显示,您可以使用以下命令获取文档的数据event.data.data()
.
然后你可以使用 JavaScript 迭代字段名称Object.keys()
方法或通过简单的数组检查来测试数据是否具有字段:
exports.tryHasChild=functions.firestore.document('cities/{newCityId}')
.onWrite((event) =>{
if (event.data.exists) {
let data = event.data.data();
Object.keys(data).forEach((name) => {
console.log(name, data[name]);
});
if (data["population"]) {
console.log("The edited document has a field of population");
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)