I'm using the plugin for NativeScript and Firebase (nativescript-plugin-firebase) to help code the backend of a mobile app that I'm working on. I have a scenario where I'm using the firebase.query, and then using it with the SingleEvent set to true (so it does not listen continuously).
The problem I'm having is trying to access the Key of one or more items, plus be able to do things like item.name, item.id, etc. I can get to this data if I use the "SingleEvent: false" setting, but not when it's set to "SingleEvent: true". I've tried doing crazy things like JSON.stringify to JSON.parse to build a new JS array, but it seems like I'm going down the wrong path as there should be an easier way (I'm hoping).
The Callback (simplified for SO):
var onQueryEvent = function (result) {
// note that the query returns 1 match at a time
// in the order specified in the query
if (!result.error) {
//FirebaseService.consoleFlowerBox("Result: " + JSON.stringify(result));
var _items = JSON.stringify(result.value);
var _itemsParse = JSON.parse("[" + _items + "]");
var items = [];
for (let rec of _itemsParse) {
items.push(rec);
}
//
// Use with Single Event Setting
//
var i = 0;
for (let singleItem of items) {
// Get the object Key
var mykey = "";
for (let k in singleItem) {
mykey = k;
}
var _item = JSON.stringify(singleItem
}
The Query:
firebase.query(
onQueryEvent,
"/" + this.c.nodeC.UPLOAD_ITEMS,
{
// true: runs once, provides all data to onQueryEvent
// false: loops onQueryEvent, row by row, continuous listening
singleEvent: true,
orderBy: {
type: firebase.QueryOrderByType.CHILD,
value: 'UID'
},
range: {
type: firebase.QueryRangeType.EQUAL_TO,
value: BackendService.token
},
}
);
A Result:
[{"-KpGrapjgsM427sd9g7w":{"selected":true,"modifiedDate":"2017-07-17","UID":"cJiaR0jgR2RYUcsp6t7PgKac9ef2","description":"234","status":"Not Uploaded","modifiedByUID":"cJiaR0jgR2RYUcsp6t7PgKac9ef2","name":"Test 3","localFullPath":"/data/user/0/com.customapp.test/files/UploadedMedia/1500317102269.png","count":2,"archived":false,"modifiedTime":"4:18:21 pm"}}]
Once I have "data" from a singleEvent: true, I would like to be able to do something like this:
(if multiple records)
for (let item of items) {
var x = item.id;
var y = item.name;
}
(if single record)
var x = item.id;
var y = item.name;
Thanks!