我有一个 Cloud Functions,它会在我的实时数据库中的某个值发生变化时触发。之后,我需要从数据库中读取另一个值。
我在网上搜索并找到了一种解决方案。它起作用了:该函数在 /ID/temp_id
的值发生变化时立即触发,但是读取 /ID/I
的值需要额外的 5 秒。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.getID = functions.database.ref("/ID/temp_id").onUpdate((change, context)=>{ //triggers the function when value "temp_id" changes
const tempID = change.after.val();
const i_snap = admin.database().ref("/ID/i").once("value", function(snapshot){ //read the value at "/ID/i" from the databse
const i = snapshot.val();
})
})
有什么方法可以更快地读取 /ID/I
的值吗?
最佳答案
一般来说,您不能像那样简单地加速简单的数据库写入。请注意,Cloud Functions 有一个 unavoidable cold start在新服务器实例上首次调用您的函数的时间。
我实际上根本不希望您的函数运行,因为您没有返回一个 promise ,该 promise 会在您的函数中完成所有异步工作时解决。你有义务这样做,这样你的 function terminates normally .
关于javascript - 如何使用 Cloud Functions 从实时数据库中读取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56718737/