Google Cloud Function无法调用firestore.get(),this._firestore.getAll不是DocumentReference.get中的函数

时间:2019-12-01 06:40:17

标签: firebase google-cloud-firestore google-cloud-functions firebase-admin

我正在尝试在Google云功能中获取Firestore文档,但是Firebase Admin SDK抛出错误,并且我不确定如何使用Admin SDK库修复错误。我已经将firebase-admin依赖项更新为^ 8.0.0,但无济于事。

TypeError: this._firestore.getAll is not a function at DocumentReference.get
 (***/functions/node_modules/@google-cloud/firestore/build/src/reference.js:198:32) at updateClaims (***/functions/index.js:49:22)

这是我的代码的相关区域,为清楚起见已缩短:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firestore = admin.firestore.Firestore; /// Tried calling admin.firestore.DocumentReference(...); as well...
admin.initializeApp();


/// ...

/// Private function member that updates the user's claims, with UID cleaned by userWriteClaims
const updateClaims = async (userid) => {
 let userIndexRef = new firestore.DocumentReference('Users/' + userid);
 return userIndexRef.get().then(documentSnapshot => {
  /// @TODO: my logic here
 })
};

const errorMessage = (error, code) => {
 /// ...
};

/// Public-facing HTTPS endpoint 
exports.userWriteClaims = functions.https.onRequest(async (request, response) => {
 /// function verifies user exists, then calls:
 response.send(await updateClaims(request.query.uid));

});

1 个答案:

答案 0 :(得分:0)

结果我忘了初始化一个新的Firestore实例,该实例需要与admin SDK分开进行。

与上述相比,这需要进行两项更改:

const firestore = new admin.firestore.Firestore;
let userIndexRef = firestore.doc('Users/' + userid);