0

我正在使用 NativeScript 和 Firebase 插件 (nativescript-plugin-firebase) 来帮助编写我正在开发的移动应用程序的后端。我有一个场景,我正在使用 firebase.query,然后在 SingleEvent 设置为 true 的情况下使用它(因此它不会连续监听)。

我遇到的问题是尝试访问一个或多个项目的密钥,并且能够执行 item.name、item.id 等操作。如果我使用“SingleEvent:false”,我可以获取这些数据设置,但在设置为“SingleEvent:true”时不会。我试过做一些疯狂的事情,比如 JSON.stringify 到 JSON.parse 来构建一个新的 JS 数组,但似乎我走错了路,因为应该有更简单的方法(我希望)。

回调(简化为 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
}

查询:

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
            },


        }
    );

结果:

[{"-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"}}]

一旦我从一个单一事件中获得“数据”:真的,我希望能够做这样的事情:

(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;

谢谢!

4

1 回答 1

1

由于缺乏文档、更好地了解 TypeScript 或两者兼而有之,我已经弄清楚如何使用nativescript-plugin-firebasefirebase.query和它的选项来处理结果集singleEvent: true

它的诀窍是使用来自 TypeScript 的let 。

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

然后根据来自 firebase的id获取对象。

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

完整的功能代码:

    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
            },
        }
    );
于 2017-07-18T19:50:20.997 回答