我正在使用firebase,并列出了一些具有某些优先级的人员.在我的一个职能中,设置和获得优先级正常.但在另一个,我只能设置并尝试获取项的优先级返回’dataSnapshot.getPriority()不是一个函数’.
var playersList = new Firebase('https://myfirebase.firebaseIO.com/players')
var winnerSnapshot = playersList.child(winner);
winnerSnapshot.setPriority('1300'); //This is working
var oldPriority = winnerSnapshot.getPriority(); //Not working
最佳答案 实际上这里有两种不同类型的物体.一个
Firebase引用和一个
DataSnapshot.当你调用新的Firebase()时,你会得到一个Firebase引用,它允许你写入数据(例如使用set或setPriority)或附加回调来读取数据(例如使用on或一次).
这些使用on()或once()注册的回调通过DataSnapshot接收数据,您可以在其上调用.getPriority().查看Reading Data文档了解完整详情.
例如,为了使您的示例有效,您可以执行以下操作:
var winner = "somebody";
var playersListRef = new Firebase('https://myfirebase.firebaseIO.com/players')
var winnerRef = playersListRef.child(winner);
// You use a firebase reference to write data.
winnerRef.setPriority('1300');
// You can also use a firebase reference to attach a callback for reading data.
winnerRef.once('value', function(winnerSnapshot) {
// Inside your callback, you get a DataSnapshot that gives you access to the data.
var priority = winnerSnapshot.getPriority();
});