在Firebase方法中同步处理功能

时间:2019-07-13 09:20:31

标签: firebase ionic-framework

我要提取已登录用户的详细信息,并根据用户定义页面设置。但是由于异步特性,我无法先提取详细信息并进行比较。在服务文件的definepages()中,身份验证功能应先运行,然后再对userEmail进行比较。可以做到这一点。任何帮助将不胜感激。


<ion-header>
    <ion-toolbar class="page-toolbar">
        <ion-buttons slot="start">
            <ion-menu-button></ion-menu-button>
          </ion-buttons>
      <ion-title>My Order Page</ion-title>
    </ion-toolbar>
  </ion-header>

  <ion-content padding>
    <ion-item *ngFor="let u of user">
        <ion-card>
    <ion-item *ngFor="let o of order">
    <ion-card>
        <ion-avatar slot="start">
            <img [src]= o.productImg />
        </ion-avatar>
        <ion-card-content> Image : {{ o.image}}  </ion-card-content>
        <ion-card-content> Product Key : {{ o.productKey}}  </ion-card-content>
        <ion-card-content> Quantity: {{ o.orderQty}}  </ion-card-content>
        <ion-card-content> Cost : {{ o.TotalValue}} </ion-card-content>
        <ion-card-content> DeliveryStatus: {{ o.status}} </ion-card-content>
        <ion-card-content> Delivery Date: {{ o.DeliveryDate}} </ion-card-content>
        <ion-card-content hidden> TransactionKey: {{ o.transactionKey}} </ion-card-content>
        <ion-button *ngIf='u.userType!="Admin"'(click)="acceptOrder(o.productKey, o.orderQty, o.TotalValue, o.status, o.DeliveryDate, o.transactionKey)">ACCEPT</ion-button>
        <ion-button *ngIf='u.userType!="Client"' >REJECT</ion-button>
        <ion-button *ngIf='u.userType!="Client"'>TRANSFER</ion-button>
        <ion-button>RETURN</ion-button>
        <ion-button>ACKNOWLEDGE</ion-button>
     </ion-card>
    </ion-item>
  </ion-card>
  </ion-item>

  </ion-content> 

ts文件

import { Component, OnInit } from '@angular/core';
import { order } from './my-order-page.model'; // importing the interface ; we will use this interface fields 
import * as firebase from 'firebase';
import { User } from 'src/app/pages/menu/menu.model';
//import { AngularFireDatabase } from "angularfire2/database";
import { OrderService } from 'src/app/services/myorders.service';
@Component({
  selector: 'app-my-order-page',
  templateUrl: './my-order-page.page.html',
  styleUrls: ['./my-order-page.page.scss'],
})
export class MyOrderPagePage implements OnInit {
  user:User[];
  order: order[];

  constructor(private orderService : OrderService) { 
    this.order = [];
  }

  ngOnInit() {
   // window.location.reload();
    this.user=this.orderService.definepages();
    this.order =this.orderService.getMyOrders();
    //this.order =this.orderService.getMyOrders();
  }

  acceptOrder(productKey, orderQty, TotalValue, status, DeliveryDate, transactionKey){

   this.orderService.acceptOrder(productKey, orderQty, TotalValue, status, DeliveryDate, transactionKey); 
  }
}

服务文件

   import { Injectable } from '@angular/core';
import * as firebase from 'firebase';
import { order } from '../pages/my-order-page/my-order-page.model';
import { AngularFireDatabase } from "angularfire2/database";
import { DatePipe } from '@angular/common';
import { User } from '../pages/menu/menu.model';
@Injectable({
  providedIn: 'root'
})
export class OrderService {

   User = [];
   orders = [];
   orderRef = firebase.database().ref().child('Transaction');
   usersRef = firebase.database().ref().child('Users');
   constructor() { 
   this.orders =[];

  }
  definepages(){ 
    var pages=[];
    var userid="";
    var usersRef = firebase.database().ref().child('Users');
    var userEmail = "";
    var userType = "";
    // while(true)
    // {
   firebase.auth().onAuthStateChanged(function (user) {
     if (user) {
        console.log(user.email);
        userEmail = user.email;
       userid=user.uid;
       } else {
         //userEmail = "0";
        // No user is signed in.
      }
    });

  this.usersRef.on('value', snapshot => {
    while(this.User.length)
   {this.User.pop();}   
   snapshot.forEach(childSnapshot => {
    console.log(userEmail);
     if (childSnapshot.val().email == userEmail)
     {

     let item = childSnapshot.val();
     item.key = childSnapshot.key;
     this.User.push(item);
     }
  });  
  });

  // if(userEmail == "0")
  // break;
//}
  // return the entire array; avaoid using spread operator
  console.log(this.User);
  return this.User;

  }

  getClientOrders()
  {


    this.orders=[];
    this.orderRef.on('value', snapshot => {
      while(this.orders.length)
      {
        this.orders.pop();
      }

      snapshot.forEach(childSnapshot => {
        if (childSnapshot.val().isActive == 'Y') //&& (childSnapshot.val().sender == emailID || childSnapshot.val()receiver == emailID))
        {
        let item = childSnapshot.val();
        item.key = childSnapshot.key;

        this.orders.push(item);
        }

    });  
    });

    console.log(this.orders)
    return this.orders;
  }


0 个答案:

没有答案