如何从Firebase功能访问Firestore [英] how to access firestore from firebase functions

查看:34
本文介绍了如何从Firebase功能访问Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经启动了一个无服务器Web应用程序的Firebase项目.从客户端,我可以访问Firestore数据库.从无服务器方面,我编写了一个在http请求上调用的函数.该函数正在尝试通过Firestore对象访问数据库,但由于Firestore对象没有我认为应该具有的collection()函数,因此它失败了.在输出中,我显示了Firestore对象的内容.

I have started a Firebase project for a serverless web application. From the client-side I can access the Firestore database. From the serverless-side I have written a function to be called on http request. The function is trying to access the database by means of a Firestore object but it is failing because the Firestore object has no collection() function as I think it should have. In the output I show the content of the Firestore object.

const functions = require('firebase-functions');

exports.noteList = functions.https.onRequest((req, res) => {
  db = functions.firestore;
  console.dir(db);
  db.collection("notes").listDocuments().then(documentRefs => {
   return db.getAll(documentRefs);
  }).then(documentSnapshots => {
   res.json(documentSnapshots);
  });
});

输出:

{ provider: 'google.firestore',
  service: 'firestore.googleapis.com',
  defaultDatabase: '(default)',
  document: [Function: document],
  namespace: [Function: namespace],
  database: [Function: database],
  _databaseWithOpts: [Function: _databaseWithOpts],
  _namespaceWithOpts: [Function: _namespaceWithOpts],
  _documentWithOpts: [Function: _documentWithOpts],
  DatabaseBuilder: [Function: DatabaseBuilder],
  NamespaceBuilder: [Function: NamespaceBuilder],
  snapshotConstructor: [Function: snapshotConstructor],
  beforeSnapshotConstructor: [Function: beforeSnapshotConstructor],
  DocumentBuilder: [Function: DocumentBuilder] }
Function crashed
TypeError: db.collection is not a function

为进行比较,这是我从客户端访问数据库的方式,这是有效的:

For comparison this is how I access the database from the client-side, this is working:

function main() {
  var db = firebase.firestore();
  db.collection("global").doc("public").get().then(
    function(r) {
      var number_span = document.getElementById("number_span");
      data = r.data();
      number_span.textContent = "" + data.counter;
    });
}

当然,firebase对象是通过不同的方式获得的.也许缺少某些配置?

Of course the firebase object is obtained in different ways. Maybe some configuration is missing?

推荐答案

您需要使用Firestore SDK(通常通过 Firebase Admin SDK )以访问Firestore.Cloud Functions SDK(firebase-functions)不会为您执行此操作.它所做的只是帮助您指定要部署的功能.该函数的主体应使用Firestore SDK.

You need to use a Firestore SDK (typically via the Firebase Admin SDK) to access Firestore. The Cloud Functions SDK (firebase-functions) does not do this for you. All it does is help you specify the functions you want to deploy. The body of the function should use the Firestore SDK.

// require and initialize the admin SDK
const admin = require('firebase-admin');
admin.initializeApp();

// now use the SDK in the body of the function
admin.firestore().collection(...).doc(...)

Admin SDK仅包装了 Cloud Firestore节点SDK ,因此请使用其参考来浏览API.

The Admin SDK just wraps the Cloud Firestore Node SDK, so use its reference to navigate the APIs.

这篇关于如何从Firebase功能访问Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆