当有人被邀请参加“项目”时,我会触发一个小的云功能。应该将通知保存在/users/{id}/annotations
中。
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import {sendAnnotation} from '../utils/AnnotationService'
// Looks up whether an invited user is already registrated.
// If yes: Sends an Email and leaves an annotation in /users/{id}/annotations
// If no: Sends an Email and registers a ghost user with an annotation in
/users/{id}/annotations
export default functions.firestore
.document('/projects/{projectid}/invitations/{invitationId}')
.onCreate((snap, context) => {
const data = snap.data()
let email = data.email
let sentBy = data.sentBy
let createdAt = data.createdAt
let invitedTo = context.params.projectid
admin.firestore()
.collection('users/')
.where("email", "==", email)
.get()
.then((usersQuerySnapshot) => {
if ( usersQuerySnapshot.empty ) {
//Invited User not found in Database
console.log('Invited User not registrated')
} else {
//Invited User found in Database
usersQuerySnapshot.forEach( userSnapshot => {
let userId = userSnapshot.id
let payload = {
type: 'projectInvitation',
meta: {
invitedTo: invitedTo,
createdAt: createdAt,
sentBy: sentBy,
state: 'pending',
expires: ''
},
isRead: 'false',
}
console.log(admin)
sendAnnotation(admin, userId, payload)
})
}
})
.catch((err) => console.log(err))
})`
这一切都很好,没有错误。关键部分是如下定义的“ sendAnnotation(admin,userId,payload)”:
export function sendAnnotation(admin, userId, payload) {
console.log('Invitation sent to ' + userId + ' with payload: ' + JSON.stringify(payload))
admin.firestore
.collection(`/users/${userId}/annotations`)
.add(payload)
.catch( err => {
console.log(err.message || err)
})
}
在这里,firebase控制台打印admin.firestore.collection is not a function
。由于admin.firestore.collection的语法与我想知道的文档中的语法完全一样,因此,传递启动的admin对象是否存在任何问题。
有什么想法吗?
答案 0 :(得分:0)
您显示的代码的两位不完全相同。第一次使用admin SDK时,您将其命名为:
admin.firestore().collection(...)
第二次使用它时,您将其命名为:
admin.firestore.collection(...)
admin.firestore
是一个函数,而不是属性,因此您需要将其称为一个函数。