title: DataSnapshot


wilddog.sync.DataSnapshot 是当前指定节点下的数据快照,DataSnapshot 不会随当前节点数据的变化而发生改变。我们无法直接创建这个对象,而应当在 ononce 的回调函数中来获取它。

方法

exists

定义

exists()

说明

判断当前 DataSnapshot 实例中是否包含数据。使用 exists() 方法进行非空判断比 snapshot.val() !== null 更高效。

返回值

Boolean

示例

假设我们有以下数据:

  1. {
  2. "name" : {
  3. "first" : "Jim",
  4. "last" : "Gordon"
  5. }
  6. }

exists 检测是否包含特定子节点:

  1. wilddog.sync().ref("/samplechat/users/jim").once("value")
  2. .then(function(snapshot) {
  3. var a = snapshot.exists();
  4. // a === true
  5. var b = snapshot.child("name").exists();
  6. // b === true
  7. var c = snapshot.child("name/first").exists();
  8. // c === true
  9. var d = snapshot.child("name/middle").exists();
  10. // d === false ,因为 "name/middle" 节点下没有数据。
  11. });

val

定义

val()

说明

返回当前数据快照包含的数据。val() 可能返回的数据类型包括:String、Number、Boolean、null(该节点下数据为空时)或者是数组、对象。返回的数据类型取决于节点下的数据内容。

返回值

Object|String|Number|Boolean|null 当前数据快照包含的数据, null 表示该节点的数据快照为空。

示例

假设我们已经有如下的数据:

  1. {
  2. "pm25":42
  3. }
  1. var ref = wilddog.sync().ref("/city/Beijing/pm25")
  2. ref.on('value', function(snapshot){
  3. // 打印的内容为:Then pm25 of Beijing is 42
  4. console.log('The pm25 of Beijing is ', snapshot.val());
  5. });

child

定义

child(path)

说明

根据相对路径,来获取当前节点下子节点的数据快照。相对路径可以是一个字节点的 key 值(如:"Beijing"),也可以是更深层次的路径(如:'Beijing/pm25')。如果相对路径下并没有数据,则返回 null 。

参数
参数名 说明
path String(non-null)类型
path 为相对路径,多层级间需要使用"/"分隔,例如"a/b"
返回值

wilddog.sync.DataSnapshot 实例

示例

假设我们已经有如下的数据:

  1. {
  2. "pm25":42
  3. }
  1. var ref = wilddog.sync().ref("/city/Beijing");
  2. ref.on('value',function(snapshot){
  3. var pm25 = snapshot.child('pm25');
  4. // 打印内容为:Then pm25 of Beijing is 42
  5. console.log("The pm25 of Bejing is", pm25.val())
  6. })

forEach

定义

forEach(callback)

说明

遍历数据快照中的每一个子节点。受 JavaScript Object 对象的影响,snapshot 直接通过 val() 方法返回的数据不能保证显示的顺序完全符合 orderBy*() 的要求,因此我们提供了 forEach() 方法来解决这个问题。如果没有使用 orderBy*() 方法,依次遍历出来的结果默认选择 key 排序(除非数据快照设置过 priority ,则会根据 priority 排序)。

参数
参数名 说明
callback function(non-null)类型
遍历每一个子节时的回调函数。如果在 callback 中主动 return true 则会停止之后的遍历。
返回值

Void

示例

假设我们已经有如下的数据:

  1. {
  2. "users": {
  3. "Jim": {
  4. "first": "Jim",
  5. "last": "Gordon"
  6. },
  7. "Alan": {
  8. "first": "Alan",
  9. "last": "Turing"
  10. }
  11. }
  12. }
  1. var query = wilddog.sync().ref("users").orderByKey();
  2. query.once("value")
  3. .then(function(snapshot) {
  4. snapshot.forEach(function(childSnapshot) {
  5. // key will be "Alan" the first time and "Jim" the second time
  6. var key = childSnapshot.key();
  7. // childData will be the actual contents of the child
  8. var childData = childSnapshot.val();
  9. });
  10. });
  1. // 如果在 callback 中返回 true,则会停止之后的遍历过程。
  2. var query = wilddog.sync().ref("users").orderByKey();
  3. query.once("value")
  4. .then(function(snapshot) {
  5. snapshot.forEach(function(childSnapshot) {
  6. var key = childSnapshot.key(); // "Jim"
  7. // 取消遍历
  8. return true;
  9. });
  10. });

hasChild

定义

hasChild(key)

说明

判断是否存在某个指定的子节点。如果指定节点下的数据不为空,则返回 true。

参数
参数名 说明
key String(non-null)类型
要检查的 key 。
返回值

Boolean

示例

假设我们已经有如下的数据:

  1. {
  2. "name": {
  3. "first": "Jim",
  4. "last": "Gordon"
  5. }
  6. }
  1. // Determine which child keys in DataSnapshot have data.
  2. var ref = firebase.database().ref("users/Jim");
  3. ref.once("value")
  4. .then(function(snapshot) {
  5. var hasName = snapshot.hasChild("name"); // true
  6. var hasAge = snapshot.hasChild("age"); // false
  7. });

hasChildren

定义

hasChildren()

说明

如果 Datasnapshot 存在子节点返回 true,否则返回 false。你可以通过使用 hasChildren() 方法来确定当前的数据快照是否含有子节点,进而决定是否调用 forEach() 方法来遍历数据。

返回值

Boolean

示例

假设我们已经有如下的数据:

  1. {
  2. "name": {
  3. "first": "Jim",
  4. "last": "Gordon"
  5. }
  6. }

hasChildren 检测 DataSnapshot 是否包含任何子节点:

  1. var ref = wilddog.sync().ref("/users/jim");
  2. ref.once("value").then(function(snapshot) {
  3. var a = snapshot.hasChildren(); // a === true
  4. var b = snapshot.child("name").hasChildren(); // b === true
  5. var c = snapshot.child("name/first").hasChildren(); // c === false
  6. }).catch(function(err)){
  7. console.error('get value failed', err);
  8. });

key

定义

key()

说明

返回当前数据快照所属节点的 key。

返回值

String

示例

假设我们已经有如下的数据:

  1. {
  2. "pm25":42
  3. }
  1. var ref = wilddog.sync().ref("/city/Beijing/pm25")
  2. ref.on('child_changed', function(snapshot){
  3. if(snapshot.hasChild('pm25')){
  4. var pm25 = snapshot.child('pm25');
  5. var key = snapshot.key();
  6. // The pm25 of Beijing is 42.
  7. console.log("The ", pm25.key(), " of Bejing is", pm25.val());
  8. };
  9. })

numChildren

定义

numChildren()

说明

返回子节点的个数。

返回值

Number

示例

假设我们已经有如下的数据:

  1. {
  2. "name": {
  3. "first": "Jim",
  4. "last": "Gordon"
  5. }
  6. }
  1. var ref = wilddog.sync().ref("/users/Jim");
  2. ref.once('value', function (snapshot) {
  3. var a = snapshot.numChildren();
  4. // a === 1 ("name")
  5. var b = snapshot.child("name").numChildren();
  6. // b === 2 ("first", "last")
  7. var c = snapshot.child("name/first").numChildren();
  8. // c === 0 (since "Jim" is a String)
  9. }).catch(function(err){
  10. console.error('operation is failed ', err);
  11. })

ref

定义

ref()

说明

返回当前数据快照所关联的 wilddog.sync.Reference 实例。

返回值

wilddog.sync.Reference

示例

假设我们已经有如下的数据:

  1. {
  2. "pm25":42
  3. }
  1. var ref = wilddog.sync().ref("/city/Beijing");
  2. ref.once('value', function(snapshot){
  3. console.log(snapshot.val()); // {'pm25',42}
  4. var newRef = snapshot.ref();
  5. newRef.once('value', function (newSnapshot) {
  6. console.log(newSnapshot.val()); // 42
  7. })
  8. })

getPriority

定义

getPriority()

说明

获取当前节点的 priority 值。如果优先级不存在时返回 null。

返回值

String|Number|null 不存在优先级时返回 null。

示例
  1. var ref = wilddog.sync().ref("/samplechat/users");
  2. ref.setWithPriority("fred", 500, function(error) {
  3. ref.once("value", function(snapshot) {
  4. var priority = snapshot.getPriority();
  5. // priority === 500
  6. });
  7. });

exportVal

定义

exportVal()

说明

DataSnapshot 中的全部内容导出到 JavaScript 对象。exportVal() 方法和 val() 方法类似,都可以导出数据。但是当节点的 priority 值不为空时,exportVal() 会导出包含 priority 的数据,适合用于备份。

返回值

数据快照的值和优先级。

示例
  1. ref.setWithPriority("hello", 500).then(function(){
  2. return ref.once('value');
  3. }).then(function(snap){
  4. var data = snapshot.exportVal();
  5. // data is { ".value": "hello", ".priority": 500 }
  6. // data[".value"] === "hello"
  7. // data[".priority"] === 500
  8. }).catch(function(err) {
  9. console.error(err)
  10. });