gpt4 book ai didi

javascript - 类型 'data' 上不存在属性 '() => DocumentData'

转载 作者:行者123 更新时间:2023-11-30 20:32:36 25 4
gpt4 key购买 nike

我刚开始使用 Firebase Cloud Functions,我偶然发现了这个源代码 https://github.com/AngularFirebase/93-fcm-ionic-demo/blob/master/functions/src/index.ts .我已经为该项目下载了所有必需的依赖项,但在使用 event.data.data() 时我一直收到错误消息。这是我的代码:

import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);


exports.newSubscriberNotification = functions.firestore
.document('subscribers/{subscriptionId}')
.onCreate(async event => {

const data = event.data.data();

const userId = data.userId
const subscriber = data.subscriberId

// Notification content
const payload = {
notification: {
title: 'New Subscriber',
body: `${subscriber} is following your content!`,

}
}

// ref to the parent document
const db = admin.firestore()
const devicesRef = db.collection('devices').where('userId', '==', userId)


// get users tokens and send notifications
const devices = await devicesRef.get()

const tokens = []

devices.forEach(result => {
const token = result.data().token;

tokens.push( token )
})

return admin.messaging().sendToDevice(tokens, payload)

错误应该是在 const data= event.data.data() 上。我完全确定这是使用它的正确方法,但因此我无法部署我的功能。我已经检查过,两个 package.json 文件(一个在我的根项目文件夹中,一个在 functions 文件夹中)上都有最新版本的云函数。有人知道可能导致此问题的原因吗?非常感谢您的帮助。

最佳答案

云功能更新了,更新带来了很多变化。

因此,您需要将以下内容更改为:

exports.newSubscriberNotification = functions.firestore.document('subscribers/{subscriptionId}').onCreate((snap,context) => {

const data = snap.data();

更多信息在这里:

https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore

关于javascript - 类型 'data' 上不存在属性 '() => DocumentData',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com