我想使用firebase函数在RealTime数据库上创建,编辑,读取和删除。在查看其他类似问题时,我看到必须使用AdminSdk,所以我做到了。我基本上复制/粘贴了相同的Firebase指南提供的代码。
const admin = require("firebase-admin");
const functions = require("firebase-functions");
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});
const db = admin.database();
db.ref("devices")
.once("value")
.then(snapshot => console.log("Snapshot: ",snapshot.val())
.catch(error => console.log(error))
});
在初始化过程中,我使用applicationDefault()设置了凭据,就像之前使用service_account_key.json路径设置了GOOGLE_APPLICATION_CREDENTIALS env变量一样。无论如何,我尝试使用cert方法进行设置,结果没有改变。由于“服务帐户”部分显示了3个帐户,因此我也尝试了所有这些帐户。
这就是说,从控制台使用“ firebase serve”启动功能时,不会显示日志,也不会出现错误。
有什么我想念的吗?一些进一步的配置或您可能会意识到的任何错误?
先感谢您!
根据您的评论进行更新:
如您的问题所示,您要“使用Cloud Functions在实时数据库上创建,编辑,读取和删除”,以模仿客户端SDK的行为,但要从您控制的服务器进行。您应该使用直接从该服务器调用的一个或多个Cloud Functions。根据您的评论,最合适的方法是使用HTTPS Cloud Function。
例如,您可以使用HTTPS Cloud Function(类似于下面的简单函数)来写入Realtime Database的特定节点,如下所示:
exports.writeToNode = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const dbNode = req.body.nodeRef;
const objToWrite = req.body.nodeValue;
return admin.database().ref(dbNode).push(objToWrite)
.then(() => {
return res.send("Node " + dbNode + " updated!");
})
.catch(err => {
//please watch the official video https://www.youtube.com/watch?v=7IkUgCLr5oA&t=1s&list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM&index=3
});
});
});
您可以通过向以下URL发出POST来进行调用https://us-central1-YOURPROJECTID.cloudfunctions.net/writeToNode
,其正文如下:
{
nodeRef: 'theNode',
nodeValue: {
firstName: 'John',
lastName: 'Doe'
}
}
初始化Admin SDK:
如果您想通过Cloud Function与同一Firebase项目中的Realtime Database进行交互,则只需初始化Admin SDK而无需任何参数(即admin.initializeApp();
)
这样,Admin SDK将使用项目的默认服务帐户,并将具有对实时数据库的完全访问权限(即绕过所有安全规则)。
因此,初始化如下:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
/////其他想法/////
请注意,您可能会使用实时数据库公开的REST API,而不是通过Cloud Functions开发整套CRUD端点。请参阅https://firebase.google.com/docs/database/rest/start
初始答案的其余部分,关于背景触发的云函数
然后,您需要通过以下方式声明一个Cloud Function,如下例所示:
exports.makeUppercase = functions.database.ref('/devices/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});
从文档复制而来的此代码段将侦听在该devices
节点下创建的任何新节点,并将以大写形式创建该uppercase
节点的值original
。
请注意,这是后台触发的云功能,当特定路径“发生”某事时触发。
如您的问题所示,如果要“在RealTime数据库上创建,编辑,读取和删除”,以模仿Client SDK的行为,则可以定义一个或多个直接从App调用的Cloud Functions。请参阅Callable Cloud Functions文档。
您还可以阅读以下文档项目https://firebase.google.com/docs/functions/get-started和https://firebase.google.com/docs/functions/database-events,还可以观看视频系列:https://firebase.google.com/docs/functions/video-series
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句