我正在尝试根据子值matchID从Firebase实时数据库查询数据。
下面是我的matchPredictions表的JSON导出,其中只有一个条目。
{
"-LP0aVMsmqfdHj7yfPnK" : {
"matchID" : "-LP0LKl_nR4VQf6Gxwz8",
"matchKickoff" : "2019-01-12T12:00",
"teamAName" : "Germany",
"teamAScore" : "2",
"teamBName" : "Denmark",
"teamBScore" : "3",
"userId" : "RZVbxcIB1SWxj0ohgJDqkEU9ia13"
}
}
我有以下被称为..
的函数export const fetchPredictionsForCompletedMatch = (match, token) => {
console.log("Match is " +match);
console.log("matchPredictionsFBRef is " + matchPredictionsFBRef);
const retrievePreds = matchPredictionsFBRef.orderByChild("matchID").equalTo(match).on("value",
function(snapshot){
console.log("Snapshot here is "+ snapshot);
});
return dispatch => {
retrievePreds.then(
response => dispatch(addMatchResultSuccess(match))
)
.catch(
err => dispatch(addMatchResultFail(err))
)
}
}
match的值是预期的-LP0LKl_nR4VQf6Gxwz8,matchPredictionsFBRef是https://projectOne.firebaseio.com/matchPredictions, 这是我要查询的表。
运行此错误时,将引发错误“错误是TypeError:retrievePreds.then不是函数”,并且快照未打印到控制台。
任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
Firebase的on()
方法可以多次给出结果。由于承诺只能解决一次,因此on()
方法无法返回承诺。
如果您只想获得一次结果,请使用Firebase的once()
方法而不是on()
:
const retrievePreds = matchPredictionsFBRef.orderByChild("matchID").equalTo(match).once("value",
...
答案 1 :(得分:0)
可以在链接之前调用该函数吗?
retrievePreds()。then()