0

I am trying to send a time difference between two distances from my NodeJS server to Firebase database.

function sendDatabase(dbName, payload1) {
    var timeNow = getTime();
    console.log(timeNow);
    var ref = db.ref(dbName);
    ref.set({difference:payload1});
}

dBName = IpBBEfCob0c1GaYkvAzog9rVdKn1/

payload1 is the time difference that I want to send.

dbName is a child node in the database and I want to save payload1 as child node of dbName every single time it executes.

But every time this function executes it will just replace it and no new entry will be created. I know that this is because the property name difference is the same every time, therefore I searched on Google to see if I can make it unique so that it will not replace, but I could not find a proper answer to my issue.

I am a beginner to Firebase and help with this would be much appreciated.

5
  • Have you tried push()? firebase.google.com/docs/reference/js/… Commented May 30, 2017 at 12:15
  • yes i tried push. But then it creates a unique ID node and saves just one instance under it. The next time it will create another node and save. What i want is to have everything in the same node Commented May 30, 2017 at 12:17
  • 1
    You want multiple instances of the same difference key? Commented May 30, 2017 at 12:17
  • yes that is exactly what i want. but it shud be under the same dbName node Commented May 30, 2017 at 12:18
  • Instead, grab a reference to the difference node and push() children into it. push is chronologically ordered, so the unique key nodes will appear in order and you can filter by LimitToLast(2) to grab the last 2 nodes. Commented May 30, 2017 at 12:21

1 Answer 1

3

You can't have multiple instances of the same key. Then it's not a key anymore. You can't do this:

dbName
 - difference : 00
 - difference : 01
 - difference : 02
 - difference : ...

What you can do is have multiple children of difference:

dbName
  + difference
    - khghjgfvhgfh : 00
    - khghjgfvhgfi : 01
    - khghjgfvhgfj : 02
    - khghjgfvhgfk : ...

By pushing unique children:

db.ref(dbName).child("difference").push(payload1)

push automatically orders children chronologically.

And when you query your nodes, LimitToLast, First, etc...

db.ref(dbName).child("difference").limitToLast(2)
Sign up to request clarification or add additional context in comments.

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.