我创建了这个简单的函数(根据本指南:https://medium.com/@jamesmarino/getting-started-with-react-native-and-firebase-ab1f396db549)
export function getNameOfUserToKill(userID, gameID, callback) {
let path = '/games/' + gameID + '/' + userID;
fb
.database()
.ref(path)
.on('value', snapshot => {
var nameOfUserToKill = '';
if (snapshot.val()) {
nameOfUserToKill = snapshot.val().userToKill;
}
callback(nameOfUserToKill);
});
}
但我不知道如何最终得到数据......
我应该nameOfUserToKill = getNameOfUserToKill(USER1234, GAME65754)
吗?我不知道“回调”是什么意思。谢谢你的帮助。
所以我可以做到:
var nameOfUserToKill = '';
export function getNameOfUserToKill(userID, gameID) {
let path = '/games/' + gameID + '/' + userID;
fb
.database()
.ref(path)
.on('value', snapshot => {
nameOfUserToKill = '';
if (snapshot.val()) {
nameOfUserToKill = snapshot.val().userToKill;
}
});
}
var user = nameOfUserToKill;
答案 0 :(得分:1)
要检索Firebase数据库的内容:
return $.ajax({
type: "POST",
url: "myurl",
data: "myid"
});
let path = '/games/' + gameID + '/' + userID;
fb.database().ref(path).on('value', snapshot => {
var nameOfUserToKill = '';
if (snapshot.val()) {
nameOfUserToKill = snapshot.val().userToKill;
}
将包含检索到的数据。