简体   繁体   English

如何在Cloud函数中从Firebase数据库检索多个值?

[英]How can you retrieve multiple values from Firebase database in cloud functions?

I have the following function that I did in Java, it solves my problem, however I want to play with javascript as well. 我有以下在Java中执行的功能,它可以解决我的问题,但是我也想玩JavaScript。 So I had the following problem: I only found functions that retrieve unique values, and I wanted a function similar to dataSnapshot.getChildren() that has in Java only that for JavaScript, if not, what would be the alternative? 因此,我遇到了以下问题:我只找到了检索唯一值的函数,并且想要一个类似于dataSnapshot.getChildren()的函数,该函数仅在Java中具有JavaScript的功能,如果没有,那有什么替代方法?

To understand better, I'll leave the Java code here that works perfectly. 为了更好地理解,我将在此处保留完美的Java代码。

mRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            float okk = Float.valueOf(ds.child("value").getValue(String.class))/100000000;
                            prov += ds.child("wallet").getValue(String.class)+", "+String.format(Locale.US,"%.8f", okk)+"\n";
                            ds.getRef().removeValue();
                        }
                        tx_array.setText(prov);
                    }
                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

The output will have to look like this, the same is in java. 输出必须看起来像这样,在Java中也是如此。

39Hs93m61zYCaiaNe8yzgrDcutVAz2Kgdc, 0.00151515 3QMTHAaYcQB8kJxF5nxxBwskyCFukCNH8t, 0.00151515 3AcNSeB9DX3ZKvGxMaec9uZ98rY2BJKuzW, 0.00153787 36SjF1MBm2DE6YimNYiy9T4ez6Z7UA4rpg, 0.001540903 AHr3GF12div1Kgf6DegeiHSGQYssvbmih, 0.00162121 19vR7xchAg1vUgGwATwBsz5NYrVWYKdSQ3, 0.00164545 3KmfDgW9RdWp7P2ns3tydXsiChR5U9XKdT, 0.00165757 1C8rxppQk8mRSWB8xPKZ5DsYVykJBLNhV3, 0.00166212 39Hs93m61zYCaiaNe8yzgrDcutVAz2Kgdc,0.00151515 3QMTHAaYcQB8kJxF5nxxBwskyCFukCNH8t,0.00151515 3AcNSeB9DX3ZKvGxMaec9uZ98rY2BJKuzW,0.00153787 36SjF1MBm2DE6YimNYiy9T4ez6Z7UA4rpg,0.001540903 AHr3GF12div1Kgf6DegeiHSGQYssvbmih,0.00162121 19vR7xchAg1vUgGwATwBsz5NYrVWYKdSQ3,0.00164545 3KmfDgW9RdWp7P2ns3tydXsiChR5U9XKdT,0.00165757 1C8rxppQk8mRSWB8xPKZ5DsYVykJBLNhV3,0.00166212

Database Struct 数据库结构

数据库结构

If you take this piece of Java code: 如果采用这段Java代码:

mRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            float okk = Float.valueOf(ds.child("value").getValue(String.class))/100000000;
            prov += ds.child("wallet").getValue(String.class)+", "+String.format(Locale.US,"%.8f", okk)+"\n";
            ds.getRef().removeValue();

The equivalent in JavaScript would be: JavaScript中的等效项是:

ref.once("value").then(function(snapshot) {
    snapshot.forEach(function(ds) {
        var okk = ds.child("value").val() / 100000000;
        prov += ds.child("wallet").val()+ ", "...
        ds.ref.remove();
    });
});

If you run into this type of question more often, I highly recommend reading the Android documentation and Web documentation side by side. 如果您经常遇到此类问题,我强烈建议您并排阅读Android文档Web文档 They both follow the exact same structure, so once you know how to do something in Android, you can easily map it to JavaScript. 它们都遵循完全相同的结构,因此一旦您知道如何在Android中进行操作,就可以轻松将其映射到JavaScript。

Based on what I read and the user told me, I was able to solve my problem with the following code, which works perfectly as I wanted. 根据我阅读的内容以及用户告诉我的内容,我可以使用以下代码解决我的问题,该代码可以按我的意愿完美运行。

exports.showEvent = functions.https.onRequest((req, res) => {
    let prov = "";
    return admin.database().ref('requests').once('value', (snapshot) => {
      snapshot.forEach(function(ds) {
          var okk = ds.child("value").val() / 100000000;
          prov += ds.child("wallet").val()+ ", " + parseFloat(okk).toFixed(8) + "\n";
        ds.ref.remove();
      });
        res.send(prov);
     });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用来自 firebase 的 momentjs 检索多个值? - How can you retrieve multiple values with momentjs from firebase? 如何从 Cloud Functions 访问和操作 Firebase 数据库? - How can I reach and manipulate Firebase Database from Cloud Functions? 在一个云功能中从 firebase 数据库中检索多个数据 - Retrieve multiple data from firebase database in one cloud function 如何在 Firebase Cloud Functions 中确认 PubSub 消息? - How can you acknowledge a PubSub message in Firebase Cloud Functions? 如何在 Cloud Functions for Firebase 中访问多个实时数据库实例 - How to access multiple Realtime Database instances in Cloud Functions for Firebase 如何使用Cloud函数中的indexOn从实时数据库检索数据 - How to retrieve data from realtime database using indexOn in cloud functions Firebase的Cloud Functions:如何从数据库同步读取数据? - Cloud Functions for Firebase: how to read data from Database synchronously? 如何使用 firebase 云功能删除多个路径 - how can i delete multiple paths with firebase cloud functions 如何使用Firebase云功能查询数据库 - How to query database with firebase cloud functions 如何从Firebase数据库检索单个JSON值? - How to Retrieve individual JSON values from Firebase Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM