0

I'm trying to write data to the Firebase Database with a given Priority, in order to then retrieve the childs in reversed order than they are currently in. The following I got from the answer of another question regarding this issue:

var postData = {
    url: urlvar
  };

  // Get a key for a new Post.
  var newPostKey = firebase.database().ref().push();//.key;
  newPostKey.setWithPriority(postData, 0 - Date.now());

  var updates = {};

  updates['/user-posts/' + uid + '/' + '/urls/' + newPostKey] = postData;

  return firebase.database().ref().update(updates);

However, when trying to trigger this, I'm getting (from the extensions popup console):

Error: Firebase.update failed: First argument contains an invalid key

Which argument is meant by that, what exactly is wrong here?

What did work, before I tried to set a priority, was simply the code above but newPostKey was attached a key:

var newPostKey = firebase.database().ref().push().key;
Community
  • 1
  • 1
ffritz
  • 2,180
  • 1
  • 28
  • 64

1 Answers1

1

After trying around different approaches I have found a solution where I can just add the priority to the postData, and then write it to the DB:

  var postData = {
    url: urlvar,
    ".priority":  0 - Date.now()
  };

  var newPostKey = firebase.database().ref().push().key;
Community
  • 1
  • 1
ffritz
  • 2,180
  • 1
  • 28
  • 64
  • 1
    Note that (while still functional and not deprecated) there is no reason to use priorities for this. Adding a property named `invertedData` you can accomplish the same and get a more explicit data structure (`.priority` is hidden from most views/values). – Frank van Puffelen Aug 14 '16 at 15:27