const functions = require('firebase-functions');
const admin = require('firebase-admin');
const FieldValue = require('@google-cloud/firestore')
admin.initializeApp();
exports.collectEmail = functions.auth.user().onCreate((user) => {
const email = user.email;
admin.firestore().collection('all_emails').doc('emails').update({
email: FieldValue.arrayUnion(email)
})
})
我仍然收到错误:FieldValue.arrayUnion不是函数
答案 0 :(得分:3)
您正在使用标识符firebase
,而没有先定义它。那是无效的。
您似乎从Firestore JavaScript SDK复制了代码,然后将其粘贴到使用Fireabse Admin SDK与Firestore配合使用的地方。这些是不同的SDK。您仅应在服务器端代码(包括Cloud Functions)上使用Admin SDK。
如果要使用FieldValue from the admin SDK,则需要从导入的管理SDK中引用它:
admin.firestore.FieldValue
不幸的是,这还行不通,因为管理SDK尚未使用支持arrayUnion的Firestore节点SDK版本,即0.16.x。
因此,您需要执行的操作是强制升级到最新的@google-cloud/firestore
程序包(由Admin SDK包装)
npm install @google-cloud/firestore@latest
然后将其导入您的代码中
import { FieldValue } from '@google-cloud/firestore'
然后在您的代码中使用新导入的版本:
email: FieldValue.arrayUnion(email)
答案 1 :(得分:0)
您应该在package.json
文件中签入最新版本的“ firebase-admin”,即版本6.0.0(在编写此答案时)。
请参阅Admin Node.js SDK发行说明here,该发行说明指出,使用此版本,已升级到Cloud Firestore客户端,其中包含FieldValue.arrayUnion()
和FieldValue.arrayRemove()
API 。