【问题标题】:Property 'data' does not exist on type '() => DocumentData'类型“()=> DocumentData”上不存在属性“数据”
【发布时间】:2018-10-15 07:25:14
【问题描述】:

我刚开始使用 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 文件(我的根项目文件夹中的一个和函数文件夹中的一个)上都有最新版本的云函数。有人知道可能导致此问题的原因吗?非常感谢您的帮助。

【问题讨论】:

    标签: javascript typescript firebase google-cloud-functions


    【解决方案1】:

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

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

    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

    【讨论】:

    • 非常感谢!我已经阅读了这些文档,但我还没有看到我还必须改变我必须初始化 firebase-admin 的方式。现在一切正常:)
    【解决方案2】:

    我有同样的问题。一切都是正确的,但仍然无法正常工作,部署时出现错误。我通过在“compilerOptions”的 tsconfig.json 中添加 "strict": false, 来解决。我希望这对某人有所帮助。

    【讨论】:

      【解决方案3】:

      代码示例:

      const user = userDoc.data();
      if (user) {
        const { firstName, lastName } = user;
      }
      

      你需要检查你的值不是undefined

      【讨论】:

        猜你喜欢
        • 2021-10-14
        • 1970-01-01
        • 2023-01-19
        • 1970-01-01
        • 1970-01-01
        • 2018-01-23
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多