我正在使用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;
謝謝!