0

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!

piercove
  • 811
  • 9
  • 17

1 Answers1

1

With the lack of documentation, knowing TypeScript better, or a combination of both, I have figured out how to get through a result set by using the nativescript-plugin-firebase, firebase.query and it's option of singleEvent: true.

The trick to it is using let from TypeScript.

// Loop through the results
for (let id in result.value) {...}

And then getting the object based on the id from firebase.

// Get the object based on the id 
let res = (<any>Object).assign({ id: id }, result.value[id]);

Complete Function Code:

    var that = this;
    var onQueryEvent = function (result) {
        // note that the query returns 1 match at a time
        // in the order specified in the query
        if (!result.error) {
            //
            // Use with singleEvent: true
            //

            // Loop through the result(s)
            for (let id in result.value) {
                // Get the object based on the id 
                let res = (<any>Object).assign({ id: id }, result.value[id]);

                if (res.selected && res.status != "Review") {

                    // Update Upload Record
                    var _newInfo = {
                        'archived': true,
                        'selected': false,
                        'status': "Archived"
                    }
                    FirebaseService.updateData(_newInfo, "/CUSTOM_NODE_EXT_HERE", id)
                        .then(
                        function () {
                            // Callback, additional functions, etc.
                        },
                        function (errorMessage: any) {
                            console.log(errorMessage);
                        });
                }
            }
        }
    };

    firebase.query(
        onQueryEvent,
        "/CUSTOM_NODE_NAME_HERE",
        {
            // 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
            },
        }
    );
piercove
  • 811
  • 9
  • 17