简体   繁体   English

Firebase快照参考不会返回数据

[英]Firebase snapshot reference won't return data

I'm using firebase and have a list of people, with certain priorities. 我正在使用Firebase,并列出了具有某些优先级的人员。 In one of my functions, setting and getting priorities is working fine. 在我的一项职能中,设置和获取优先级工作正常。 But in another, I can only set and trying to get the priority of the item returns 'dataSnapshot.getPriority() is not a function'. 但是在另一种方法中,我只能设置并尝试获取该项目的优先级,并返回“ 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

There are actually two different types of object at play here. 实际上,这里有两种不同类型的对象在玩。 A Firebase reference, and a DataSnapshot . Firebase参考和DataSnapshot When you call new Firebase(), you get a Firebase reference which allows you to write data (eg using set or setPriority) or attach callbacks for reading data (eg using on or once). 当您调用new Firebase()时,您将获得一个Firebase引用,该引用允许您写入数据(例如,使用set或setPriority)或附加用于读取数据的回调(例如,使用on或一次)。

These callbacks registered with on() or once() receive the data via a DataSnapshot and you can call .getPriority() on that . 这与()或一次()注册的回调通过DataSnapshot接收数据,并且可以调用.getPriority() Check out the Reading Data docs for full details. 查看Reading Data文档以获取全部详细信息。

For example, to make your example work, you could do something like: 例如,要使示例工作,可以执行以下操作:

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM