2017-07-17 6 views
0

NativeScriptとFirebase(nativescript-plugin-firebase)のプラグインを使用して、作業中のモバイルアプリケーションのバックエンドをコーディングします。私はfirebase.queryを使用しているシナリオを持っているし、それをtrueに設定されたSingleEventで使用しているので、連続して聞こえません。NativeScript/Typescript/Firebase singleEventがtrueのときにデータを取得して使用するプラグイン

私が抱えている問題は、1つまたは複数のアイテムのKeyにアクセスして、item.name、item.idなどのことを実行できるようにしようとしていることです。このデータには、 SingleEvent:false "に設定されますが、" SingleEvent:true "に設定されている場合は無効になります。 JSON.parseのようなクレイジーなことを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"}}] 

私は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を使用して結果セットを取得する方法を考え出した、firebase.query、それはオプションですシングルイベント:true

このトリックはを使用しています。をTypeScriptから取得してください。

// 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 
      }, 
     } 
    ); 
関連する問題