我結束了與singleEvent = true使用firebase.query()。爲此,我創建了一個名爲一次()函數:
once(path, key) {
return firebase.query(
()=>{},
path,
{
singleEvent: true,
orderBy: {
type: firebase.QueryOrderByType.KEY
},
ranges: [{
type: firebase.QueryRangeType.EQUAL_TO,
value: key
}],
limit: {
type: firebase.QueryLimitType.FIRST,
value: 1
}
})
.then(
queryResult => {
// A query result contains a dictionary containing all objects that matched the query criteria. As we are interested on obtaining a single object, we can assume that dictionary will have only a single element.
//
let queryValue = queryResult.value;
let keys = Object.keys(queryValue);
if (keys.length > 0) {
let key = keys[0]
let value = queryValue[key];
return Promise.resolve(value);
} else {
return Promise.resolve(null);
}
}
)
}
這可以這樣使用:
once("/players", userId).then(userData => {console.log(userData)})
我不知道我喜歡這種方法非常多。似乎使用查詢來訪問特定的節點可能是無益的。我仍在尋找更好的解決方案。謝謝。