财产数据'类型'()=>上不存在DocumentData'

时间:2018-05-04 18:29:12

标签: typescript firebase google-cloud-functions

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

3 个答案:

答案 0 :(得分:1)

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

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

$(document).ready(function() {
$("#a-stop").click(function(e){
  $("#a-start-container").html("<a id='a-start' href=''><i class='fa fa-fw fa-play inner-circle-color'></a>");
  $("#a-stop-container").html("<i class='fa fa-fw fa-stop inner-circle-color-off'>");
  })
});

此处提供更多信息:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

代码示例:

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

您需要检查您的值不是undefined

相关问题