如何使用离子3连接Firebase中的两个对象(不使用AngularFire)?

时间:2017-10-23 23:14:58

标签: angular typescript firebase ionic-framework firebase-realtime-database

我有Firebase的这种数据结构。

https://i.stack.imgur.com/wcwU9.png

list.html

https://i.stack.imgur.com/D4nO0.png

在这里,我尝试解析为每个引用创建侦听器的对象,但确实显示了任何内容。我做错了什么?

list.ts



import { Component, ViewChild } from '@angular/core';
import { NavController, NavParams, LoadingController, ViewController, } from 'ionic-angular';
import { ItemDetailsPage } from '../item-details/item-details';
import firebase from 'firebase';

@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})
export class ListPage {
  organizationUsersArray: Array<OrganizationUserObject> = []
  listInvitedUserArray: Array<UserPublicInfoObject> = []
  //foodRatings: FirebaseListObservable<any[]>;

  public itemRef: firebase.database.Reference = firebase.database().ref('organization_user/user_public_info/');
  public itemRef2: firebase.database.Reference = firebase.database().ref('users_public_info/');
  //public itemRef3: firebase.database.Reference  = firebase.database().ref('organization_users/' + UserPublicInfoObject.user + '/name');
  dbref2 : firebase.database.Reference;
  listenerValueChanged: any
  listenerValueDeleted: any
  listenerValueAdded: any
  listenerValueChanged2: any
  listenerValueDeleted2: any
  listenerValueAdded2: any

  constructor(public navCtrl: NavController, public navParams: NavParams, public loadingCtrl: LoadingController, public view: ViewController) {
    /*
    this.itemRef = this.navParams.data;
    this.foodRatings = firebase.database.ref('/rateItems/'+this.navParams.data.$key);
    */
  }
  
  ionViewDidEnter() {
    this.initzializeData();
  }
  ionViewDidLeave() {
    
    this.itemRef2.child(firebase.auth().currentUser.uid).off('child_changed', this.listenerValueChanged2)
    this.itemRef2.child(firebase.auth().currentUser.uid).off('child_added', this.listenerValueAdded2);
    this.itemRef2.child(firebase.auth().currentUser.uid).off('child_removed', this.listenerValueDeleted2);

    this.itemRef.child(firebase.auth().currentUser.uid).off('child_changed2', this.listenerValueChanged);
    this.itemRef.child(firebase.auth().currentUser.uid).off('child_added2', this.listenerValueAdded);
    this.itemRef.child(firebase.auth().currentUser.uid).off('child_removed2', this.listenerValueDeleted);

  }
  initzializeData() {
  
   this.organizationUsersArray.forEach(element => {
        this.dbref2 = this.itemRef2.child(element.idUser);
   
    });

    try {
      console.log(firebase.auth().currentUser.uid)
      var id = firebase.auth().currentUser.uid
      var dbref = this.itemRef.child(id);
      this.dbref2 = this.itemRef2.child(id);
     
    } catch (error) {
      console.log("Usuario no loggeado");
    }
    //Listeners_Add
    this.listenerValueAdded = dbref.on('child_added', itemSnapshot => {
      // itemSnapshot.forEach( itemSnap => {
      // this.items.push(itemSnap.val());
      console.log(itemSnapshot.val().name)
      var idUser: string = itemSnapshot.key
      var archive: boolean = itemSnapshot.val().archive
      var createdAt: string = itemSnapshot.val().createdAt
      var status: string = itemSnapshot.val().status
      var userId: UserPublicInfoObject = itemSnapshot.val().userId
      this.organizationUsersArray.push(new OrganizationUserObject(idUser, archive, createdAt, status, userId));
      return false;
      //  });
    });
    //Listeners_Add 2 
    this.listenerValueAdded2 = this.dbref2.on('child_added', itemSnapshot => {
      var publicUserId: string = itemSnapshot.val().userId
      var email: string = itemSnapshot.val().email
      var name: string = itemSnapshot.val().name
      var photoUrl: string = itemSnapshot.val().photoUrl
      this.listInvitedUserArray.push(new UserPublicInfoObject(publicUserId, email, name, photoUrl));
      return false;
      //  });
    });
    //Child_changed 
    this.listenerValueChanged = dbref.on('child_changed', itemSnapshot => {
      console.log(itemSnapshot.val().name)
      var idUser: string = itemSnapshot.key
      var archive: boolean = itemSnapshot.val().archive
      var createdAt: string = itemSnapshot.val().createdAt
      var status: string = itemSnapshot.val().status
      var userId:UserPublicInfoObject = itemSnapshot.val().userId
      var i = 0
      var tmpObject = new OrganizationUserObject(idUser, archive, createdAt, status, userId);
      //this.showUpdatedItem(tmpObject)
      this.organizationUsersArray.forEach(element => {
        if (element.idUser == idUser) {
          this.organizationUsersArray[i] = tmpObject
          //this.showUpdatedItem(tmpObject)
        }
        i = i + 1
      });
      return false;
    });

    //Child changed 2
    this.listenerValueChanged2 = this.dbref2.on('child_changed', itemSnapshot => {
      var publicUserId: string = itemSnapshot.val().key
      var email: string = itemSnapshot.val().email
      var name: string = itemSnapshot.val().name
      var photoUrl: string = itemSnapshot.val().photoUrl
      this.listInvitedUserArray.push(new UserPublicInfoObject(publicUserId, email, name, photoUrl));
      var x = 0;
      var tmpObject2 = new UserPublicInfoObject(publicUserId, email, name, photoUrl);

      this.listInvitedUserArray.forEach(element => {
        if (element.publicUserId == publicUserId) {
          this.listInvitedUserArray[x] = tmpObject2
          //this.showUpdatedItem(tmpObject)
        }
        x = x + 1
      });
      return false;
      //  });
    });
    //Child_removed
    this.listenerValueDeleted = dbref.on('child_removed', itemSnapshot => {
      console.log(itemSnapshot.val().name)
      var idUser: string = itemSnapshot.key
      var archive: string = itemSnapshot.val().archive
      var createdAt: string = itemSnapshot.val().createdAt
      var status: string = itemSnapshot.val().status
      var userId: UserPublicInfoObject = itemSnapshot.val().userId
      var i = 0
      var tmpObject = new OrganizationUserObject(idUser, archive, createdAt, status, userId);
      for (var i = 0; i < this.organizationUsersArray.length; i++) {
        if (this.organizationUsersArray[i].idUser == idUser) {
          this.organizationUsersArray.splice(i, 1);  //removes 1 element at position i 
          break;
        }
      }
      return false;
    });
    //Child_removed 2
    this.listenerValueDeleted2 = this.dbref2.on('child_removed', itemSnapshot => {
      var publicUserId: string = itemSnapshot.val().key
      var email: string = itemSnapshot.val().email
      var name: string = itemSnapshot.val().name
      var photoUrl: string = itemSnapshot.val().photoUrl
      this.listInvitedUserArray.push(new UserPublicInfoObject(publicUserId, email, name, photoUrl));
      var x = 0;
      var tmpObject2 = new UserPublicInfoObject(publicUserId, email, name, photoUrl);

      this.listInvitedUserArray.forEach(element => {
        if (element.publicUserId == publicUserId) {
          this.listInvitedUserArray[x] = tmpObject2
          //this.showUpdatedItem(tmpObject)
        }
        x = x + 1
      });
      return false;
      //  });
    });

  }//End initializeData
  showUpdatedItem(newItem) {
    let updateItem = this.organizationUsersArray.find(this.findIndexToUpdate, newItem.idUser);
    let index = this.organizationUsersArray.indexOf(updateItem);
    this.organizationUsersArray[index] = newItem;
  }
  showUpdatedItem2(newItem2) {
    ///Ref2
    let updateItem2 = this.listInvitedUserArray.find(this.findIndexToUpdate2, newItem2.publicUserId);
    let index2 = this.listInvitedUserArray.indexOf(updateItem2);
    this.listInvitedUserArray[index2] = newItem2;
  }
  findIndexToUpdate(newItem) {
    return newItem.idUser === this;
  }
  findIndexToUpdate2(newItem2) {
    return newItem2.publicUserId === this;
  }
  //Image case validation
  typeImageByNameChecList(photoUrl) {
    var tmpUrl = ""
    switch (photoUrl) {
      case "":
        photoUrl = "assets/images/iso9001.jpg"
        break;

      default:
        tmpUrl = "assets/images/iso9001.jpg"
        break;
    }
    return tmpUrl
  }
}
class OrganizationUserObject {
  idUser: string
  archive: string
  createdAt: string
  status: string
  userId: UserPublicInfoObject

  constructor(idUser, archive, createdAt, status, userId) {
    this.idUser = idUser
    this.archive = archive
    this.createdAt = createdAt
    this.status = status
    this.userId = userId

  }
}
class UserPublicInfoObject {
  publicUserId: string
  email: string
  name: string
  photoUrl: string
  constructor(publicUserId, email, name, photoUrl) {
    this.publicUserId = publicUserId
    this.email = email
    this.name = name
    this.photoUrl = photoUrl

  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
&#13;
&#13;
&#13;

请帮助我,在没有AngularFire的情况下正确显示数据。

0 个答案:

没有答案