错误:类型“ DataSnapshot”上不存在属性“ getChildren”

时间:2018-08-03 07:54:47

标签: javascript angular firebase firebase-realtime-database

...它在文档P中有

import * as firebase from "firebase";位于文件顶部。 uid-fetching函数起作用。由于某些原因,它不喜欢for循环的语法...

我还尝试了语法:for (DataSnapshot child : parent.getChildren()) { },然后编译器告诉我在for循环开始的行中应该有分号。

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 个答案:

答案 0 :(得分:2)

您需要使用订阅来监视更改。使用AngularFire监视它们何时登录并获取UID(假设您在Firebase中使用身份验证登录名,以便使用UID作为树路径保存所有数据

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