gpt4 book ai didi

javascript - 如何从云功能内部运行查询?

转载 作者:数据小太阳 更新时间:2023-10-29 04:01:49 26 4
gpt4 key购买 nike

我想在调用我的 Firebase 应用程序上的云函数后对我的数据库执行查询。

假设我在数据库上有一个特定的触发器,请考虑 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);
});

如果有的话,我应该导入哪些模块?如何在数据库上执行查询?

预先感谢您的回答!

最佳答案

您可以使用 Node.js Admin SDK为此:

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!
});
});

关于javascript - 如何从云功能内部运行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com