简体   繁体   English

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

[英]error: Property 'getChildren' does not exist on type 'DataSnapshot'

...It does in the documentation P: ...在文档P中确实如此:

I import * as firebase from "firebase"; import * as firebase from "firebase"; at the top of the file. 在文件的顶部。 The uid-fetching function works. uid-fetching函数起作用。 It doesn't like the syntax of the for-loop for some reason... 由于某些原因,它不喜欢for循环的语法...

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. 我也尝试过语法: 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;
        });
    });
}

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 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM