Fre*_*Xie 3 javascript firebase google-cloud-functions google-cloud-firestore
我正在研究一些适用于 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");
}
});
Run Code Online (Sandbox Code Playgroud)
正如有关为 Cloud Functions 使用 Cloud Firestore 触发器的文档所示,您可以使用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");
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6176 次 |
| 最近记录: |