类型“() => DocumentData”上不存在属性“data”

Dan*_*dez 5 javascript firebase typescript google-cloud-functions

我刚刚开始使用 Firebase Cloud Functions,并且遇到了此源代码https://github.com/AngularFirebase/9 ​​3-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)
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*dad 6

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

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

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

const data = snap.data();
Run Code Online (Sandbox Code Playgroud)

更多信息请参见:

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