error: Property 'getChildren' does not exi

2020-02-16 02:40发布

问题:

...It does in the documentation P:

I import * as firebase from "firebase"; at the top of the file. The uid-fetching function works. It doesn't like the syntax of the for-loop for some reason...

I have also tried the syntax: for (DataSnapshot child : parent.getChildren()) { } and then the compiler tells me a semicolon is expected in the line where the for-loop starts.

getMessages() {
    return new Promise(function (resolve) {
        return firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                resolve(user.uid);
            }
        });
    }).then((result) => {
        return firebase.database().ref('mailboxes/' + result).once('value').then((snapshot) => {
            let messageArray;
            for (let snap of snapshot.getChildren()) {
                messageArray.push(snap.val());
                console.log('snapshot key:' + snap.key);
                console.log('snapshot val:' + snap.val());
            };
            return messageArray;
        });
    });
}

回答1:

You need to use a subscription to watch for the changes. Use AngularFire to watch for when they are logged in and get the UID (assuming you are using the Authentication login in Firebase so that all data is saved using the UID as the tree path

import { AngularFirestore } from 'angularfire2/firestore';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { switchMap, map } from 'rxjs/operators';
import { Observable,  pipe } from 'rxjs';
import { Observable, Subscription } from 'rxjs';
import firebase as firebase from 'firebase/app';

private myOAuthSubscription: Subscription;
private myDatasubscription: Subscription;    
  public userloggedin:boolean = false;
  public uid:string = '';

public this.items:any = [];

constructor(
  public _DB: AngularFireDatabase,
  public _afAuth: AngularFireAuth,
) {


try {
  this.myOAuthSubscription = this._afAuth.authState.subscribe(user => {

    if (user && user.uid) {

      console.log('loggedin = true');
      this.userloggedin = true;
      this.uid = String(user.uid);

      this.funDoDB():

    } else {

    console.log('loggedin = false');
    this.userloggedin = true;
    this.uid = '';

    }
  });
} catch (e) {
  console.error("fbData_subscription", e);
}



}

ngOnDestroy() {
  this.myOAuthSubscription.unsubscribe();
  this.myDatasubscription.unsubscribe();
}


private funDoDB(){
      if(this.userloggedin == true){
      try {

    //subscription using AngulaFire
    this.myDatasubscription = this._DB.list('mailboxes/' + this.uid).snapshotChanges().pipe(map(actions => {
        return actions.map(action => ({ key: action.key, val: action.payload.val() }));
      }))
      .subscribe(items => {

        this.items = [];
        this.items = items.map(item => item);

        console.log("db results",this.items);

        var icount=0;

        for (let i in this.items) {

         console.log("key",this.items[i].key);
         console.log("val",this.items[i].val); 
         console.log("----------------------------------);

         //checking if something exists
         if (this.items[i].key == 'SomeNodePath') {
           var log = this.items[i].val;
         }

        }


      } catch (e) {
        console.error(e);
      }


      });
    }
    }

npm install --save angularfire2 firebase
npm install -D rxjs@6.2.2 rxjs-compat@6.2.2