2017-07-02 66 views
0

我想從firebase返回所有符合今天日期的預訂。Firebase查詢得到今天的預訂傳回陣列

我在我的html中使用* ngFor命令來打印出所有返回的訂單。

在firebase數據庫中,我保存了7個預訂。 7箇中有2個是今天的日期。所以全部7個都在屏幕上打印出來。但是,當我點擊我的按鈕來運行以下功能時,我只想顯示今天的預約日期。

todayBookings() { 

    this.ordersData = this.af.list('/orders', { 

     query: { 
      orderByChild: 'start', 
      // equalTo: today 
     } 
    }); 
    this.ordersData.subscribe((res) => { 
     this.orders = res; 
    }); 

    this.orders.forEach(order => { 

     var d = new Date(); 
     var curr_date = ("0" + (d.getDate() + 0)).slice(-2); 
     var curr_month = ("0" + (d.getMonth() + 1)).slice(-2) 
     var curr_year = d.getFullYear(); 
     // get today's date 
     var today = curr_year + "-" + curr_month + "-" + curr_date; 
     // get date of booking 
     var dateOfBooking = order.start.substr(0, 10); 

     if (dateOfBooking == today) { 
      this.todaysOrders = order; 
      var index = this.orders.indexOf(order, 0); 
      if (index > -1) { 
       this.orders.splice(index, 1); 
      } 
      // console.log(this.orders); 
      // console.log(this.todaysOrders); 

     } 

    }); 

    this.orders = this.todaysOrders 
    console.log(this.orders); 
} 

當我在控制檯登錄以下內容:console.log(this.todaysOrders);

我在控制檯中看到2個預約今天:

screenshot of console

不幸的是,我不知道現在該怎麼走this.todaysOrders並將其傳遞迴this.orders。

我嘗試以下:

this.orders.push(this.todaysOrders);

但它帶回了所有的條目,並附上今天的預訂列表,而不是7條返回帶來了9.(所有預訂+今天的2日預訂)。

訂單數據庫

orders 
-Ko2a0zwYPQc-ocfJ1cF 
    createdAt: 1499003887000 
    orderId: 34681 
    partySize: "1" 
    requirements: "none" 
    start: "2017-07-02T15:00:44+01:00" 
    status: "Accepted" 
    type: "tablebooking" 
    + userDetails 
    userId: "CQbDG6H8ENStlZiaNOGNsvbw2GL2" 

    -Ko2ay19E7b17UhZ9HAf 
    -Ko2pmavUZNTKdsr0pv6 
    -Ko2t6cm35uOtiROeghG 
    -Ko2tn6iRmkG7Y-OfAcJ 
    -Ko2u5FrD5ZnIphL9Vno 
    -KoBtilv2-dj-XmQSQBf 

這是全班同學。我已經刪除了我用於頁面上其他內容的其他功能。

import { Component, OnInit, AfterViewInit, ElementRef, ViewChild, OnDestroy, OnChanges } from '@angular/core'; 
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database'; 
import { Router, ActivatedRoute } from "@angular/router"; 
import { NgForm } from '@angular/forms'; 
import { AngularFireAuth } from 'angularfire2/auth'; 
import * as firebase from 'firebase'; 
import { ToastrService } from 'toastr-ng2'; 
import * as moment from 'moment'; 


@Component({ 
    selector: 'app-calendar', 
    templateUrl: './calendar.component.html', 
    styleUrls: ['./calendar.component.scss'] 
}) 

export class CalendarComponent implements OnInit, AfterViewInit, OnChanges { 



    order: Array<any>; 
    orders: Array<any>; 
    ordersData: FirebaseListObservable<any>; 

    todaysOrders: Array<any>; 

    color: any; 
    orderId: any; 


    @ViewChild('fullcalendar') fullcalendar: ElementRef; 


    constructor(public af: AngularFireDatabase, public toastr: ToastrService, public router: Router, public authentication: AngularFireAuth) { 


     this.ordersData = af.list('/orders', { 
      query: { 
       orderByChild: 'start', 
      } 
     }); 
     this.ordersData.subscribe((res) => { 
      this.orders = res; 
     }); 


    } 




    //Get Todays Bookings 
    todayBookings() { 

     this.ordersData = this.af.list('/orders', { 

      query: { 
       orderByChild: 'start', 
       // equalTo: today 
      } 
     }); 
     this.ordersData.subscribe((res) => { 
      this.orders = res; 
     }); 

     this.orders.forEach(order => { 

      var d = new Date(); 
      var curr_date = ("0" + (d.getDate() + 0)).slice(-2); 
      var curr_month = ("0" + (d.getMonth() + 1)).slice(-2) 
      var curr_year = d.getFullYear(); 
      // get today's date 
      var today = curr_year + "-" + curr_month + "-" + curr_date; 
      // get date of booking 
      var dateOfBooking = order.start.substr(0, 10); 

      if (dateOfBooking == today) { 
       this.todaysOrders = order; 
       var index = this.orders.indexOf(order, 0); 
       if (index > -1) { 
        this.orders.splice(index, 1); 
       } 
       // console.log(this.orders); 
       console.log(this.todaysOrders); 

      } 

     }); 

     // this.orders = this.todaysOrders 
     // console.log(this.orders); 
    } 





} 
+0

我的數據庫是火力不知道你的意思是哥們 –

+0

向我們展示什麼是你的數據庫是什麼,佈局/結構 – theblindprophet

+0

更新主貼:) –

回答

0

您可以從orders陣列中刪除今天的每個訂單。

類似:

if (dateOfBooking == today) { 
    this.todaysOrders = order; 
    var index = this.orders.indexOf(order, 0); 
    if (index > -1) { 
    this.orders.splice(index, 1); 
    } 
    console.log(this.todaysOrders); 
    console.log(this.orders); 
} 
+0

謝謝你的友好建議@然而,在控制檯中,我看到這消除了今天的訂單,我不想刪除它們,這是我需要顯示的訂單,我想刪除所有不是今天的訂單。 –

+0

也許像'this.orders = this.todaysOrders'之後的'forEach'? – Lilrom

+0

那也行不通,我很害怕。當我console.log(this.orders)我得到的數組對象不是今天預訂。當我console.log(this.todaysOrders)我回來今天的訂單,但作爲單個對象不是我需要的對象數組。我還在foreach之後添加了this.orders = this.todaysOrders,但在控制檯中出現錯誤:**錯誤:試圖區分'[object Object]'錯誤。只有數組和迭代是允許的**我用更多的代碼更新了我的問題。 –