Reputation: 849
I am trying to query data from my Firebase realtime database based on the child value matchID.
Below is the JSON export of my matchPredictions table with only one entry in it.
{
"-LP0aVMsmqfdHj7yfPnK" : {
"matchID" : "-LP0LKl_nR4VQf6Gxwz8",
"matchKickoff" : "2019-01-12T12:00",
"teamAName" : "Germany",
"teamAScore" : "2",
"teamBName" : "Denmark",
"teamBScore" : "3",
"userId" : "RZVbxcIB1SWxj0ohgJDqkEU9ia13"
}
}
I have the following function which gets called ..
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))
)
}
}
The value for match is -LP0LKl_nR4VQf6Gxwz8 as expected and matchPredictionsFBRef is https://projectOne.firebaseio.com/matchPredictions, which is the table I am trying to query.
When this runs the error "The error is TypeError: retrievePreds.then is not a function" is thrown and snapshot is not printed to the console.
Any help would be much appreciated. Thanks.
Upvotes: 0
Views: 493
Reputation: 598847
Firebase's on()
method can give results multiple times. Since a promise can only resolve once, the on()
method can't return a promise.
If you only want to get a result once, use Firebase's once()
method instead of on()
:
const retrievePreds = matchPredictionsFBRef.orderByChild("matchID").equalTo(match).once("value",
...
Upvotes: 1
Reputation: 145
Can you call the function before chaining then.
retrievePreds().then()
Upvotes: 0