网站搜索

typescript - 出现错误 InvalidPipeArgument Angular 6


以下是正在发生的事情的简要说明:

  1. 我已将我的应用程序更新到了新的 Angular 版本,从 5 更新到了 6。
  2. 我使用 ng update 命令更新了所有 packages.json 部署。

我正在使用 Angular Material 的 DatePicker 组件,通过页面上的输入,将一个 Date() 值保存到 Cloud Firestore 数据库的 birthday_date(timestamp) 字段中。

  1. 在进行依赖更新之前,数据库中的 birthday_date (timestamp) 字段返回了类型为 Date() 的值,因此我可以使用 {{ data.birthday_date | date }}。

  2. 在更新了 package.json 中的依赖项后,您在 birthday_date (timestamp) 字段中返回了 Timestamp 类型的值。

  3. 现在我需要调用 toDate() 方法来获取 Date() 对象。

    {{ data.birthday_date.toDate() | date }}

之前的返回是一个 Date() 对象:

Sat May 19 2018 20:35:12 GMT-0300

更新后,返回的是一个 Timestamp 对象:

Timestamp {seconds: 1527476400, nanoseconds: 0}

在 package.json 中更新了依赖项:

"firebase": "4.11.0", ->
"firebase": "5.0.3"


"angularfire2": "5.0.0-rc.6", ->
"angularfire2": "5.0.0-rc.9",

以下是我部分的应用程序代码:

mycomponent.component.html:

<form name="myForm" [formGroup]="myForm" class="w-100-p">
<mat-form-field fxFlex="30">
    <input matInput [matDatepicker]="picker" formControlName="birthday_date " placeholder="Date of birth">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker #picker startView="multi-year" disabled="false" touchUi="true"></mat-datepicker>
</mat-form-field>
</form>

没有在输入框中显示日期,因为你返回的是 Timestamp,而 DatePicker 组件需要一个 Date() 值。

list.component.ts:

<ng-container cdkColumnDef="birthday_date">
        <mat-header-cell *cdkHeaderCellDef mat-sort-header fxHide fxShow.gt-xs>Date of birth</mat-header-cell>
        <mat-cell *cdkCellDef="let data" fxHide fxShow.gt-xs>
           <p class="text-truncate">{{ data.birthday_date | date }}</p>
        </mat-cell>
</ng-container>

如果我不调用 toDate() 方法 {{ data.birthday_date.toDate() | date }},将会出现以下错误:

ListComponent.html:81 ERROR Error: InvalidPipeArgument: 'Unable to convert "Timestamp(seconds=1526698800, nanoseconds=0)" into a date' for pipe 'DatePipe'
    at invalidPipeArgumentError (common.js:4238)
    at DatePipe.push../node_modules/@angular/common/fesm5/common.js.DatePipe.transform (common.js:5151)
    at checkAndUpdatePureExpressionInline (core.js:10801)
    at checkAndUpdateNodeInline (core.js:11375)
    at checkAndUpdateNode (core.js:11333)
    at debugCheckAndUpdateNode (core.js:11970)
    at debugCheckRenderNodeFn (core.js:11956)
    at Object.eval [as updateRenderer] (ListComponent.html:81)
    at Object.debugUpdateRenderer [as updateRenderer] (core.js:11948)
    at checkAndUpdateView (core.js:11320)

我正在寻找的解决方案:

我需要以某种方式更改,以返回 Date() 类型的对象,而不是 Timestamp,因为这已经影响了我应用程序中的许多地方,并延迟了我的整个项目。

答案#1

我有同样的问题。我是这样解决的:

data.birthday_date.seconds * 1000 | date