REST Firebase request via qwest

Asked by At

i have a realtime database from firebase with 9386 datasets in it (but that might change in the future)

thats why i want to know whats the "key" of the last event.

thats how i'm currently trying to get it to know: (with npm module qwest)

    let order = "\u0022\u0024key\u0022";
    let dbUrl = "https://example.firebaseio.com/users.json?limitToLast=1&orderBy=";
    let end;
    qwest.get(dbUrl + order)
    .then(function(xhr, response) {
      console.log(response);
      end = response[9386]["username"];
      console.log(end);
    });

first question: why do i have to escape the quotation marks and the dollarsign?

second question: how can i get the "key" of the last item i'm checking for in the json (limitToLast=1).

json response looks like this:

{"9386":
 {
  "fromListA":"1",
  "fromListB":"0",
  "id":"9939",
  "lastChecked":"2019-05-09 03:18:05",
  "userid":"123456789",
  "username":"username"
 }
}

and i want to get the "9386" in a variabel.

1 Answers

1
Frank van Puffelen On Best Solutions
  1. Since you chose to use " to limit the dbUrl string, you can't use " inside that string's value without escaping it. A simpler way to define the string is to use ' to delimit it:

    let dbUrl = 'https://example.firebaseio.com/users.json?limitToLast=1&orderBy="$key"';
    
  2. To get the key of the object, use something like:

    Object.keys(response)[0]
    

var response = {"9386":
 {
  "fromListA":"1",
  "fromListB":"0",
  "id":"9939",
  "lastChecked":"2019-05-09 03:18:05",
  "userid":"123456789",
  "username":"username"
 }
}

console.log(Object.keys(response)[0]);