如何从云端函数内部执行查询? [英] How to run query from inside of Cloud function?
问题描述
假设我在数据库上有一个特定的触发器请考虑 Firebase入门指南中提供的示例。
//监听添加到/ messages /:pushId / original的新消息,并创建一个
//大写的/ message /:pushId / uppercase
exports.makeUppercase = functions.database.ref('/ messages / {pushId} / original')
.onWrite(event => {
//获取写入实时数据库的当前值
const original = event.data.val();
console.log('Uppercasing',event.params.pushId,original );
const uppercase = original.toUpperCase();
//我喜欢在这里进行一个查询,只是简单的检索基于提供的ID
//在
之类的函数内部执行异步任务时,必须返回一个Promise,写入Firebase实时数据库。
//在实时数据库中设置大写兄弟会返回一个Promise。
return event.data.ref.parent.child('uppercase')。set(uppercase);
});
我应该导入哪些 模块?
如何在数据库上执行查询?
预先感谢您的回答!
a> for this:
$ b $ pre $ const $ functions('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config()。firebase);
exports.makeUppercase = functions.database $ b $ .ref('/ messages / {pushId} / original')
.onWrite(event => {
return admin.database()。ref('/ other')
.orderByChild('id')。equalTo(event.params.pushId)
.once('value')。then(snapshot => ; {
// there,I queried!
});
});
I'd like to perform a query on my database once a cloud function on my Firebase app is called.
Let's say I have a certain trigger on the database, consider the example provided in the get started guide on Firebase.
// Listens for new messages added to /messages/:pushId/original and creates an
// uppercase version of the message to /messages/:pushId/uppercase
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
console.log('Uppercasing', event.params.pushId, original);
const uppercase = original.toUpperCase();
// I'D LIKE TO PERFORM A QUERY HERE, JUST A SIMPLE RETRIEVE BASED ON THE ID PROVIDED
// 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 event.data.ref.parent.child('uppercase').set(uppercase);
});
Which modules should I import, if any? How can I perform the query on the DB?
Thank you in advance for your answer!
You can use the Node.js Admin SDK for this:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.makeUppercase = functions.database
.ref('/messages/{pushId}/original')
.onWrite(event => {
return admin.database().ref('/other')
.orderByChild('id').equalTo(event.params.pushId)
.once('value').then(snapshot => {
// there, I queried!
});
});
这篇关于如何从云端函数内部执行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!