【问题标题】:Firebase query get today's bookings pass back into arrayFirebase 查询将今天的预订传回数组
【发布时间】:2017-12-05 21:48:16
【问题描述】:

我正在尝试从 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 个预订:

不幸的是,我现在不知道如何获取 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);
    }





}

【问题讨论】:

  • 我的数据库是 firebase 不知道你的意思是什么朋友
  • 向我们展示您的数据库中的内容、布局/结构
  • 更新主帖 :)

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


【解决方案1】:

您可以从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);
}

【讨论】:

  • 感谢您的友好建议@Lilrom 但是,在控制台中我看到这删除了今天的订单,我不想删除它们,这是我需要显示的订单,我想删除所有不是今天日期的订单。
  • 在您的forEach 之后可能类似于this.orders = this.todaysOrders
  • 恐怕这也没用。当我 console.log(this.orders) 我得到一组不是今天预订的对象。当我 console.log(this.todaysOrders) 我得到今天的订单,但作为单个对象而不是我需要的对象数组。我还在 foreach 之后添加了 this.orders = this.todaysOrders,但在控制台中出现错误:错误:尝试区分“[object Object]”时出错。只允许使用数组和可迭代对象我已经用你的附加代码更新了我的问题。
  • 我找到了这个有用的文档github.com/angular/angularfire2/blob/master/docs/… 你读了吗?你能分享你所有的课程吗?
  • 在主帖中添加了完整的课程。也谢谢你的链接,它有很好的信息,我一定会利用的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 2014-06-13
相关资源
最近更新 更多