11import { Component , OnInit } from "@angular/core" ;
2+ import { firestore } from "nativescript-plugin-firebase" ;
3+ // import { firestore } from "nativescript-plugin-firebase/app/firestore";
4+
25const firebase = require ( "nativescript-plugin-firebase/app" ) ;
36
47@Component ( {
@@ -15,10 +18,64 @@ export class ItemsComponent implements OnInit {
1518 firebase . initializeApp ( ) ;
1619 }
1720
18- public login ( ) : void {
19- console . log ( ">>> login" ) ;
21+ public loginAnonymously ( ) : void {
2022 firebase . auth ( ) . signInAnonymously ( )
2123 . then ( ( ) => console . log ( "Logged in" ) )
22- . catch ( err => console . log ( "Login error: " + err ) ) ;
24+ . catch ( err => console . log ( "Login error: " + JSON . stringify ( err ) ) ) ;
25+ }
26+
27+ public firestoreAdd ( ) : void {
28+ firebase . firestore ( ) . collection ( "dogs" ) . add ( { name : "Fido" } )
29+ . then ( ( docRef : firestore . DocumentReference ) => {
30+ console . log ( "Fido added, ref: " + docRef . id ) ;
31+ } )
32+ . catch ( err => console . log ( "Adding Fido failed, error: " + err ) ) ;
33+ }
34+
35+ public firestoreSet ( ) : void {
36+ firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
37+ . set ( { name : "Woofie" , last : "lastofwoofie" , date : new Date ( ) } , { merge : true } )
38+ . then ( ( ) => {
39+ console . log ( "Woofie set" ) ;
40+ } )
41+ . catch ( err => console . log ( "Setting Woofie failed, error: " + err ) ) ;
42+ }
43+
44+ public firestoreSetByAutoID ( ) : void {
45+ firebase . firestore ( ) . collection ( "dogs" ) . doc ( )
46+ . set ( { name : "Woofie" , last : "lastofwoofie" , date : new Date ( ) } )
47+ . then ( ( ) => {
48+ console . log ( "Woofie set" ) ;
49+ } )
50+ . catch ( err => console . log ( "Setting Woofie failed, error: " + err ) ) ;
51+ }
52+
53+ public firestoreUpdate ( ) : void {
54+ firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
55+ . update ( { name : "Woofieupdate" , last : "updatedwoofie" } )
56+ . then ( ( ) => {
57+ console . log ( "Woofie updated" ) ;
58+ } )
59+ . catch ( err => console . log ( "Updating Woofie failed, error: " + JSON . stringify ( err ) ) ) ;
60+ }
61+
62+ public firestoreGet ( ) : void {
63+ const collectionRef : firestore . CollectionReference = firebase . firestore ( ) . collection ( "dogs" ) ;
64+ collectionRef . get ( )
65+ . then ( ( querySnapshot : firestore . QuerySnapshot ) => {
66+ querySnapshot . forEach ( doc => {
67+ console . log ( `${ doc . id } => ${ JSON . stringify ( doc . data ( ) ) } ` ) ;
68+ } ) ;
69+ } )
70+ . catch ( err => console . log ( "Get failed, error" + err ) ) ;
71+ }
72+
73+ public firestoreDelete ( ) : void {
74+ firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
75+ . delete ( )
76+ . then ( ( ) => {
77+ console . log ( "Woofie deleted" ) ;
78+ } )
79+ . catch ( err => console . log ( "Delete failed, error" + err ) ) ;
2380 }
2481}
0 commit comments