Firebase查询将今天的预订传回数组

时间:2017-07-02 14:19:38

标签: angular typescript firebase firebase-realtime-database angularfire2

我正在尝试从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);

我在控制台中看到了今天的两次预订:

screenshot of console

不幸的是我不知道现在如何把这个。今天的订单传递给这个。订单。

我尝试了以下内容:

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





}

1 个答案:

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