【问题标题】:Angular 2 Firebase Return Object CountAngular 2 Firebase 返回对象计数
【发布时间】:2017-08-25 21:09:30
【问题描述】:

我正在使用 Angular 2 并有一个预订组件。

进行预订后,信息将发送到预订节点中的 Firebase 数据库。

如果管理员今天有任何预订,我会尝试在应用程序的主屏幕仪表板上显示。在我的组件中,我使用 Angularfire 订阅了 firebase 中的预订节点,如下所示:

this.bookingItemsData = af.database.list('/bookings');
this.bookingItemsData.subscribe((res) => {
  this.bookingItems = res;
  console.log(res);

我想获取当前日期并将其与 firebase 预订节点中给出的日期进行比较,因此为了获取当前日期,我添加了以下内容:

//get current date
    var getToday = new Date();
    var year = getToday.getFullYear();
    var month = getToday.getMonth() + 1;
    var day = getToday.getDate();
    // var time =  getToday.getTime();
    var currentDate = day + "/" + month + "/" + year;

这成功地让我获得了我需要的 currentDate。

我现在运行它以从预订中获取日期:

// Get count of bookings
    af.database.list('/bookings').subscribe((res) => {
        this.numbeOfBookings = res.length;
        res.forEach(item => {
        //get the date propery from the bookings node  
        var specialDate = (item.start);
        //transform the date property to matching format
        specialDate = this.datePipeEn.transform(new Date(item.start), 'dd/M/yyyy');

        // console.log(specialDate);
        // console.log(currentDate);

          if(specialDate === currentDate) {
          console.log(specialDate);
          } 

        });
    });

我先得到预订列表中的预订数量,

然后我遍历预订并使用 ForEach 语句将预订日期分配给变量“specialDate”

然后我通过 datePipe 运行变量以将预订日期输出匹配到所需的格式,

然后我运行一个 if 语句来查看我返回的任何日期是否与当前日期匹配。


当我 console.log 我的 specialDate 时,我得到了 2 个与今天日期匹配的日期中返回的一个日期,这很棒。

console log

但是当我做 console.log(specialDate.length)

它计算日期中的字符而不是返回“1”(与当前日期匹配的预订日期的数量。

所以我猜我返回的是一个字符串,它计算它而不是返回一个对象。如何获取与 currentDate 匹配的日期的对象的数量?


更新:

我设法使用以下代码将与当前日期匹配的唯一预订作为对象返回:

// Get count of bookings
af.database.list('/bookings').subscribe((res) => {
    this.numbeOfBookings = res.length;
    res.forEach(item => {
    //get the date propery from the bookings node  
    var specialDate = (item.start);
    //transform the date property to matching format
    specialDate = this.datePipeEn.transform(new Date(item.start), 'dd/M/yyyy');

      if(specialDate === currentDate) {
        console.log(item);
      } 

    });
});

在控制台日志中,我看到唯一的对象是与今天日期匹配的预订。

console log

但是,如果我这样做:console.log(item.length);它计算对象中的键,而不是返回我希望的数字“1”。 (与今天日期匹配的预订量)

对于返回的 1 个对象,我将如何返回数字 1?


更新

我设法得到返回的对象数,即 1 :)

这就是我所做的,我必须使每个项目成为一个对象,然后将返回的对象放入一个数组中,然后我就可以成功 console.log 我所追求的返回值! :)

 // Get count of bookings
this.test = af.database.list('/bookings').subscribe((res) => {
    this.numbeOfBookings = res.length;

    res.forEach(item => {      
      var bookingDate = (item.start); //get the date propery from the bookings node        
      bookingDate = this.datePipeEn.transform(new Date(item.start), 'dd/M/yyyy'); //transform the date property to matching format
      var myObj = {item}; // Put each returned item into an object
      var array = $.map(myObj, function (value, index) { // Create an array with the returned objects
        return [value];
      });

      if(bookingDate === currentDate) {

        var bookingsToday = array
        var bookingsCount = array.length
        // console.log(bookingsToday);
        return(bookingsCount);

      } 

    });
});

不过,我确实有最后一个问题。

如何使用 {{interpolation}} 在我的 html 模板中引用这个返回的 'bookingsCount' 值? (Angular 2)提前非常感谢。


在第一个答案时继续 cmets

new screenshot of console

这是我添加console.log(specialDate,currentDate);时的控制台截图

代码如下:

af.database.list('/bookings').subscribe((res) => {
    var i = 0 
    this.numbeOfBookings = res.length;
    res.forEach(item => {
        var specialDate = (item.start);
        specialDate = this.datePipeEn.transform(new Date(item.start),'dd/M/yyyy');
        console.log(specialDate,currentDate);
          if(specialDate === currentDate) {
            i++ 
            console.log(item)
          } 

        });
    //use you variable here
    console.log(i)
    });

【问题讨论】:

    标签: angular date firebase time angularfire


    【解决方案1】:

    你必须在 if 语句中增加一个变量,在 forEach 之后你可以使用你的变量:

    af.database.list('/bookings').subscribe((res) => {
    var i = 0 <----------- init a variable
    this.numbeOfBookings = res.length;
    res.forEach(item => {
        var specialDate = (item.start);
        specialDate = this.datePipeEn.transform(new Date(item.start),'dd/M/yyyy');
          console.log(specialDate,currentDate); <-----------
          if(specialDate === currentDate) {
            i++ <-------------- increment it each time the date is the same
            console.log(item);
          } 
    
        });
    //use you variable here
    console.log(i)
    });
    

    我建议你:

    1. 使用,angularfire(官方angular 2 firebase)你可以在github上找到,
    2. 如果您使用 angularfire,请尝试查询,并从服务器获取匹配日期列表
    3. 如果您不需要实时功能,最终的建议是退出 firebase,即便如此,尝试使用 lambda 的 AWS dynamo,或者使用 socket.io 和 Mlab(免费 mongodb 即服务)让您手忙脚乱

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多