【问题标题】:Cloud function query with typescript使用打字稿进行云函数查询
【发布时间】: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


    【解决方案1】:

    替换这个

    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++
        }
    }
    

    与

    let title = (await chartRating.where('Title', '==', 'Game').get()).size;
    

    将 where 语句替换为您需要的条件。

    【讨论】:

    • 我收到错误 - 错误 TS1005: ',' 预期。请注意它是打字稿
    • 很抱歉。 @Dharmaraj 已更正该功能。尝试使用更正后的版本(在 where 语句的参数后面有逗号)。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2019-06-28
    • 2021-05-12
    • 1970-01-01
    • 2021-05-30
    相关资源
    最近更新 更多