0

I have the following function that I did in Java, it solves my problem, however I want to play with javascript as well. 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?

To understand better, I'll leave the Java code here that works perfectly.

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.

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

Database Struct

3
  • Can u add the database structure (A screenshot)? As cannot solve what you need from your java code. Commented Oct 11, 2018 at 14:13
  • Of course, check my update on post. Thanks! Commented Oct 11, 2018 at 17:23
  • I've been able to find the solution. :D Commented Oct 11, 2018 at 18:25

2 Answers 2

2

If you take this piece of Java code:

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:

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. They both follow the exact same structure, so once you know how to do something in Android, you can easily map it to JavaScript.

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me a lot, it almost worked, but I had to make some modifications to work. But I leave here my thanks!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.