【发布时间】: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