2017-07-17 88 views
0

我正在使用NativeScript和Firebase插件(nativescript-plugin-firebase)來幫助編寫我正在處理的移動應用程序的後端。我有一個使用firebase.query的場景,然後在SingleEvent設置爲true的情況下使用它(所以它不會持續監聽)。NativeScript/Typescript/Firebase Plugin在singleEvent爲true時獲取和使用數據

我遇到的問題是試圖訪問一個或多個項目的關鍵,再加上能夠做item.name,item.id等事情。我可以得到這些數據,如果我使用「 SingleEvent:false「設置,但當它設置爲」SingleEvent:true「時不會。我嘗試過像JSON.stringify這樣的瘋狂事情來構建一個新的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"}}] 

一旦我有一個singleEvent的「數據」:是的,我希望能夠做到這樣的事情:

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

謝謝!

回答

0

隨着缺乏文檔,知道打字稿更好,或者兩者的結合,我已經想通了如何通過使用nativescript-插件,火力firebase.query結果集得到,它的選項singleEvent:true

它的訣竅是使用TypeScript中的

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

,然後根據該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 
      }, 
     } 
    ); 
相關問題