以下内容(来自React )无法工作。我在医生里待了几个小时,但都没成功。有什么想法吗?
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" });
}
);一些终端反馈:
⚠ functions[onSomeCollectionCreate(region)]: Deployment error.感谢您的阅读。
发布于 2020-11-18 13:44:48
在云函数中,为了与Firebase服务交互,您应该使用Admin,有关更多细节,请参见文档。
因此,下列各项应能发挥作用:
// 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" });
}
);注:另外两点:
ollection()方法传递带有斜杠(/)的字符串,因为Collection引用必须有奇数段。add()方法返回的承诺。有关此关键点的更多细节,请参见文档这里。https://stackoverflow.com/questions/64894156
复制相似问题