【发布时间】:2021-10-28 03:55:33
【问题描述】:
我正在尝试查询 Firestore 字段(下拉菜单)“标题”。我正在使用以下代码来获取给定用户的标题总数,但我想应用过滤器并仅在标题 == 游戏时获取计数。请帮忙。我如何修改此功能以仅在满足此条件时设置计数器。
目前它正在获取我的标题编号。
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
admin.initializeApp()
export const updateCharts =
functions.firestore.document('users/{UserId}/count/{uid}')
.onWrite(async(change, _) => await updateStats(change))
async function updateStats (change:
functions.Change<functions.firestore.DocumentSnapshot>){
const chartRating = change.after.ref.parent
let Title = 0
const docRefs = await chartRating.listDocuments()
for (const docRef of docRefs) {
const snapshot = await docRef.get()
const data = snapshot.data()
if (data !== undefined) {
Title++
}
}
const restaurantRef = chartRating.parent!
console.log('{restaurantRef.path} now has ${Title}')
await restaurantRef.update({
Title: Title,
})
}
【问题讨论】:
标签: node.js typescript google-cloud-firestore google-cloud-functions