【问题标题】:Angular 2, Error: Invalid argument 'Invalid Date' for pipe 'DatePipe'Angular 2,错误:管道“DatePipe”的参数“无效日期”无效
【发布时间】:2017-09-21 06:04:47
【问题描述】:

我正在使用 Angular 2 开发应用程序,我有两个实体:DemandeMedicamentVffMedicament

我在DemandeMedicamentVff 中还有一个名为Accepter 的按钮,当我点击etat 属性时,我希望将其更改为“accptée”,但我也想要

'Medicament.nom== DemandeMedicament.medicamentid && demandeMedicament.etat= 'Acceptée' {Medicament.quantity = Medicament.quantity-DemandeMedicament.quantity'}'

在我这样做之后,值 etat 已更改,但 quantity 未更改。

这些是我的模型:

第一个模型

export class DemandeMedicamentVff {
    constructor(
        public id?: string,
        public medicamentid?: string,
        public date?: any,
        public quantity?: number,
        public etat?: number,
        public signature?: string,
    ) { }
}

第二个模型:Medicament

export class Medicament {
    constructor(
        public id?: string,
        public nom?: string,
        public type?: string,
        public quantity?: number,
        public ref?: string,
        public datevalidite?: any,
        public dateproduction?: any,
    ) { }
}

我的组件

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Response } from '@angular/http';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs/Rx';
import { EventManager, ParseLinks, PaginationUtil, JhiLanguageService, AlertService } from 'ng-jhipster';
import { Demandemedicamentvff } from './demandemedicamentvff.model';
import { DemandemedicamentvffService } from './demandemedicamentvff.service';
import { ITEMS_PER_PAGE, Principal } from '../../shared';
import { PaginationConfig } from '../../blocks/config/uib-pagination.config';
import { MedicamentService } from '../medicament/medicament.service';
import { Medicament } from '../medicament/medicament.model';
@Component({
    selector: 'jhi-demandemedicamentvff',
    templateUrl: './demandemedicamentvff.component.html'
})
export class DemandemedicamentvffComponent implements OnInit, OnDestroy {
    currentAccount: any;
    demandemedicamentvffs: Demandemedicamentvff[];
    error: any;
    medicaments : Medicament [];
    success: any;
    eventSubscriber: Subscription;
    routeData: any;
    links: any;
    totalItems: any;
    queryCount: any;
    itemsPerPage: any;
    page: any;
    predicate: any;
    previousPage: any;
    reverse: any;
    isSaving: boolean;

    constructor(
        private jhiLanguageService: JhiLanguageService,
        private demandemedicamentvffService: DemandemedicamentvffService,
        private medicamentService :MedicamentService,
        private parseLinks: ParseLinks,
        private alertService: AlertService,
        private principal: Principal,
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private eventManager: EventManager,
        private paginationUtil: PaginationUtil,
        private paginationConfig: PaginationConfig
    ) {
        this.itemsPerPage = ITEMS_PER_PAGE;
        this.routeData = this.activatedRoute.data.subscribe(data => {
            this.page = data['pagingParams'].page;
            this.previousPage = data['pagingParams'].page;
            this.reverse = data['pagingParams'].ascending;
            this.predicate = data['pagingParams'].predicate;
        });

        this.jhiLanguageService.setLocations(['demandemedicamentvff']);
    }

    loadAll() {
        this.demandemedicamentvffService.query({
            page: this.page - 1,
            size: this.itemsPerPage,
            sort: this.sort()}).subscribe(
            (res: Response) => this.onSuccess(res.json(), res.headers),
            (res: Response) => this.onError(res.json())
        );
    }

    loadPage (page: number) {
        if (page !== this.previousPage) {
            this.previousPage = page;
            this.transition();
        }
    }

    transition() {
        this.router.navigate(['/demandemedicamentvff'], {queryParams:
            {
                page: this.page,
                size: this.itemsPerPage,
                sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
            }
        });
        this.loadAll();
    }

    clear() {
        this.page = 0;
        this.router.navigate(['/demandemedicamentvff', {
            page: this.page,
            sort: this.predicate + ',' + (this.reverse ? 'asc' : 'desc')
        }]);
        this.loadAll();
    }

    Accepter(Demandemedicamentvff,Medicament){
        this.medicamentService.query().subscribe((res: Response) => {
            this.medicaments = res.json();
            this.medicaments.forEach((Medicament,index)=>
            {
                if (Medicament.nom== Demandemedicamentvff.medicamentid && Demandemedicamentvff.etat=="Acceptée")
                {
                    Medicament.quantity = Medicament.quantity -  Demandemedicamentvff.quatite;
                    this.medicamentService.update(Medicament).subscribe((res: Medicament) =>   
                        this.onSaveSuccess(res),
                        (res: Response) => this.onError(res.json()));
                    }
                })
            },

            (res: Response) => this.onError(res.json())
        );
        Demandemedicamentvff.etat="Acceptée";

        this.demandemedicamentvffService.update(Demandemedicamentvff).subscribe((res: Demandemedicamentvff) =>  
            this.onSaveSuccess(res),
            (res: Response) => this.onError(res.json()));
    }

    private onSaveSuccess (result: Demandemedicamentvff) {
        this.eventManager.broadcast({ name: 'demandeModification', content: 'OK'});
        this.isSaving = false;
    }

    Refuser(Demandemedicamentvff){
        Demandemedicamentvff.etat="Refusée";
        this.demandemedicamentvffService.update(Demandemedicamentvff).subscribe((res: Demandemedicamentvff) => this.onSaveSuccess(res),
            (res: Response) => this.onError(res.json()));
    }

    ngOnInit() {
        this.loadAll();
        this.principal.identity().then((account) => {
            this.currentAccount = account;
        });
        this.registerChangeInDemandemedicamentvffs();
    }

    ngOnDestroy() {
        this.eventManager.destroy(this.eventSubscriber);
    }

    trackId (index: number, item: Demandemedicamentvff) {
        return item.id;
    }

    registerChangeInDemandemedicamentvffs() {
        this.eventSubscriber = this.eventManager.subscribe('demandemedicamentvffListModification',  
        (response) => this.loadAll());
    }

    sort () {
        let result = [this.predicate + ',' + (this.reverse ? 'asc' : 'desc')];
        if (this.predicate !== 'id') {
            result.push('id');
        }

        return result;
    }

    private onSuccess (data, headers) {
        this.links = this.parseLinks.parse(headers.get('link'));
        this.totalItems = headers.get('X-Total-Count');
        this.queryCount = this.totalItems;
        // this.page = pagingParams.page;
        this.demandemedicamentvffs = data;
    }

    private onError (error) {
        this.alertService.error(error.message, null, null);
    }
}

这是 medicament.service.ts 中的功能更新:

update(medicament: Medicament): Observable<Medicament> {
    let copy: Medicament = Object.assign({}, medicament);
    copy.datevalidite = this.dateUtils
        .convertLocalDateToServer(medicament.datevalidite);
    copy.dateproduction = this.dateUtils
        .convertLocalDateToServer(medicament.dateproduction);
    return this.http.put(this.resourceUrl, copy).map((res: Response) => {
        return res.json();
    });
}

如果我在没有 dateproduction 和 datavalidite 的药物上执行 Accepter 功能,一切正常,但如果我在另一种具有 datevalidite 和 dateproduction 的药物上执行此操作,我会收到此错误:

 > Error: Invalid argument 'Invalid Date' for pipe 'DatePipe'
 at InvalidPipeArgumentError.ZoneAwareError (polyfills.bundle.js:10191)
 at InvalidPipeArgumentError.BaseError [as constructor]   
 (vendor.dll.js:86902)
 at new InvalidPipeArgumentError (vendor.dll.js:8772)
 at DatePipe.transform (vendor.dll.js:87676)
 at DateUtils.convertLocalDateToServer (vendor.dll.js:104410)
 at MedicamentService.webpackJsonp../src/main/webapp/app/entities

 /medicament/medicament.service.ts.MedicamentService.update   

 (main.bundle.js:65941)
 at SafeSubscriber._next (main.bundle.js:55972)
 at SafeSubscriber.__tryOrUnsub (vendor.dll.js:588)
 at SafeSubscriber.next (vendor.dll.js:537)
 at Subscriber._next (vendor.dll.js:490)
 at Subscriber.next (vendor.dll.js:454)
 at MapSubscriber._next (vendor.dll.js:12416)
 at MapSubscriber.Subscriber.next (vendor.dll.js:454)
 at CatchSubscriber.Subscriber._next (vendor.dll.js:490)
 at CatchSubscriber.Subscriber.next (vendor.dll.js:454)
 ErrorHandler.handleError @ vendor.dll.js:60866
 vendor.dll.js:592 Uncaught InvalidPipeArgumentError

【问题讨论】:

  • 也许你的medicamentService.query().subscribe() 永远不会解决(因此你的数量没有更新)。也可能是由您的变量命名引起的(嵌套范围内有多个名为 Medicament 的变量)。
  • 我做了什么是逻辑,我不明白你的意思。如果你想试试,给我你的答案
  • 实际上,这可能是一个错误的条件:Medicament.nom == Demandemedicamentvff.medicamentid。您正在比较 ID 和名称,您确定吗?不应该是Medicament.id == Demandemedicamentvff.medicamentid吗?
  • 他们不是 ids 他们是名字但我把它命名为 medicamentid

标签: angular typescript jhipster angular2-forms angular2-services


【解决方案1】:
Medicament.quantity = Medicament.quantity -  

     Demandemedicamentvff.quatite;

属性名不好,应该是quantity

【讨论】:

  • 不,模型中写得很清楚
猜你喜欢
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 2020-09-10
  • 2016-06-23
  • 1970-01-01
相关资源
最近更新 更多