首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从云函数中访问Google集合,引用不同的Firestore集合?

如何从云函数中访问Google集合,引用不同的Firestore集合?
EN

Stack Overflow用户
提问于 2020-11-18 13:35:53
回答 1查看 36关注 0票数 0

以下内容(来自React )无法工作。我在医生里待了几个小时,但都没成功。有什么想法吗?

代码语言:javascript
复制
import firebase = require("../../node_modules/firebase");
import * as functions from "firebase-functions";

exports.onSomeCollectionCreate = functions
    .firestore
    .document("some-collection/{someCollectionId}")
    .onCreate(async(snap, context) => {
        firebase
            .firestore()
            .collection("another-collection/{anotherCollectionId}")
            .add({ some: "data" });
    }
);

一些终端反馈:

代码语言:javascript
复制
⚠  functions[onSomeCollectionCreate(region)]: Deployment error.

感谢您的阅读。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-18 13:44:48

在云函数中,为了与Firebase服务交互,您应该使用Admin,有关更多细节,请参见文档

因此,下列各项应能发挥作用:

代码语言:javascript
复制
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

exports.onSomeCollectionCreate = functions
    .firestore
    .document("some-collection/{someCollectionId}")
    .onCreate(async(snap, context) => {
        return admin.   // note the return
            .firestore()
            .collection("another-collection")
            .add({ some: "data" });
    }
);

注:另外两点:

  1. 不应该向collection()方法传递带有斜杠(/)的字符串,因为Collection引用必须有奇数段。
  2. 注意,我们返回由add()方法返回的承诺。有关此关键点的更多细节,请参见文档这里
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64894156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档