Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit a16ec21

Browse files
Datasnapshot exists() #1094
1 parent 1b7f2ae commit a16ec21

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

demo/app/main-view-model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export class HelloWorldModel extends Observable {
237237
okButtonText: "Darn!"
238238
});
239239
} else {
240+
console.log("key exists? " + result.exists());
240241
this.set("path", path);
241242
this.set("key", result.key);
242243
this.set("value", JSON.stringify(result.val()));
@@ -256,6 +257,7 @@ export class HelloWorldModel extends Observable {
256257
firebaseWebApi.database().ref(path)
257258
.once("value")
258259
.then(result => {
260+
console.log("key exists? " + result.exists());
259261
this.set("path", path);
260262
this.set("key", result.key);
261263
this.set("value", JSON.stringify(result.val()));
@@ -268,6 +270,7 @@ export class HelloWorldModel extends Observable {
268270
firebaseWebApi.database().ref(path)
269271
.once("value")
270272
.then(result => {
273+
console.log("key exists? " + result.exists());
271274
this.set("path", path);
272275
this.set("key", result.key);
273276
this.set("value", JSON.stringify(result.val()));
@@ -308,6 +311,7 @@ export class HelloWorldModel extends Observable {
308311
firebaseWebApi.database().ref(path).orderByChild(child)
309312
.once("value")
310313
.then(result => {
314+
console.log("key exists? " + result.exists());
311315
this.set("path", path);
312316
this.set("key", result.key);
313317
this.set("value", JSON.stringify(result.val()));

docs/DATABASE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ The link is for the iOS SDK, but it's the same for Android.
311311
console.log("Listener error: " + result.error);
312312
} else {
313313
console.log("Key: " + result.key);
314-
console.log("Calue: " + JSON.stringify(result.val()));
314+
console.log("key exists? " + result.exists());
315+
console.log("Value: " + JSON.stringify(result.val()));
315316
}
316317
};
317318

src/app/database/index.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { nextPushId } from "./util/NextPushId";
55
export module database {
66
export interface DataSnapshot {
77
// child(path: string): DataSnapshot;
8-
// exists(): boolean;
8+
exists(): boolean;
99
// exportVal(): any;
1010
// forEach(action: (a: DataSnapshot) => boolean): boolean;
1111
// getPriority(): string | number | null;
@@ -35,9 +35,8 @@ export module database {
3535
} else {
3636
callback({
3737
key: result.key,
38-
val: () => {
39-
return result.value;
40-
}
38+
val: () => result.value,
39+
exists: () => !!result.value
4140
});
4241
}
4342
};
@@ -74,14 +73,13 @@ export module database {
7473
return null;
7574
}
7675

77-
public once(eventType: string, successCallback?: (a: DataSnapshot, b?: string) => any, failureCallbackOrContext?: Object | null, context?: Object | null): Promise<any> {
76+
public once(eventType: string, successCallback?: (a: DataSnapshot, b?: string) => any, failureCallbackOrContext?: Object | null, context?: Object | null): Promise<DataSnapshot> {
7877
return new Promise((resolve, reject) => {
7978
firebase.getValue(this.path).then(result => {
8079
resolve({
8180
key: result.key,
82-
val: () => {
83-
return result.value;
84-
}
81+
val: () => result.value,
82+
exists: () => !!result.value
8583
});
8684
});
8785
});
@@ -93,9 +91,8 @@ export module database {
9391
callbacks && callbacks.map(callback => {
9492
callback({
9593
key: result.key,
96-
val: () => {
97-
return result.value;
98-
}
94+
val: () => result.value,
95+
exists: () => !!result.value
9996
});
10097
});
10198
};

0 commit comments

Comments
 (0)