Chart.js 使用 Angular 错误显示 firestore 中的日期无法读取 null 的属性(读取“toDate”)

问题描述 投票:0回答:1

用户从 mat datepicker 添加日期并存储在 firestore 中(例如 2023 年 10 月 15 日,时间戳类型)。这应该以 15/10/2023 的格式显示在图表的 x 轴上。我用过

          const dateLabels = this.data.map((item) => item.weightDate).sort();       
          const monthLabels =  dateLabels.map(a=>this.datePipe.transform(a.toDate(), 
                                   'dd/MM/YYYY'));  

 this.chart = new Chart('MyChart', {
        type: 'line',
         data: {
         labels: monthLabels,
         datasets: [
          {
            label: 'Weight',
            data: weightLabels,
            backgroundColor: 'blue',
          },
        ],
      },
      options: {
        aspectRatio: 2.5,        
      },

出现错误 TypeError:无法读取 null 的属性(读取“toDate”)

angular date google-cloud-firestore timestamp chart.js
1个回答
0
投票

在您的代码中使用它:

import { DateAdapter } from '@angular/material/core';

data.dateAdapter.setLocale('en-GB');
© www.soinside.com 2019 - 2024. All rights reserved.