错误:类型"DataSnapshot"上不存在属性"getChildren" [英] error: Property 'getChildren' does not exist on type 'DataSnapshot'

查看:203
本文介绍了错误:类型"DataSnapshot"上不存在属性"getChildren"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...它在文档P中确实存在:

...It does in the documentation P:

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

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...

我还尝试了语法:for (DataSnapshot child : parent.getChildren()) { },然后编译器告诉我在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.

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;
        });
    });
}

推荐答案

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

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

这篇关于错误:类型"DataSnapshot"上不存在属性"getChildren"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆