momentjs中有一个时间对象,在Web浏览器控制台上出现此错误:
[错误]错误–错误:未捕获(承诺):TypeError:date.isValid不是函数。 (在“ date.isValid()”中,“ date.isValid”未定义)
fromModel
writeValueforEach @ [本地代码]
setValueonInvoke
运行onInvokeTask
runTask
rainMicroTaskQueue
invokeTask
invokeTask
globalZoneAwareCallback错误:未捕获(承诺):TypeError:date.isValid不是函数。 (在“ date.isValid()”中,“ date.isValid”未定义)
fromModel
writeValueforEach @ [本地代码]
setValueonInvoke
运行onInvokeTask
runTask
rainMicroTaskQueue
invokeTask
invokeTask
onInvokeTaskrunTaskdrainMicroTaskQueueinvokeTaskinvokeTaskglobalZoneAwareCallbackresolvePromise(funciónanónima)
defaultErrorLogger(core.js:1873)
handleError(core.js:1922)
下一个(core.js:5008:105)
(funciónanónima)(core.js:3993)
__tryOrUnsub(Subscriber.js:262)
下一个(Subscriber.js:200)
_next(Subscriber.js:138)
下一个(Subscriber.js:102)
下一个(Subject.js:64)
发射(core.js:3985)
运行(zone.js:137)
onHandleError(core.js:4365)
runGuarded(zone.js:153)
_loop_1(zone.js:676)
microtaskDrainDone(zone.js:685)
rainMicroTaskQueue(zone.js:601)
invokeTask(zone.js:499)
invokeTask(zone.js:1539)
globalZoneAwareCallback(zone.js:1565)
所有这些都在用户组件中,但是来自另一个名为extendedUser的实体,该实体在extedededUser DTO中具有一个oneToone关系。一些设置日期的功能,我尝试在用户服务上实现它,我认为我缺少一些东西,我在两者上都进行了控制台登录并显示了相同的数据对象,所以我认为错误在于服务
扩展用户的服务,可以很好地显示日期
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import * as moment from 'moment';
import { DATE_FORMAT } from 'app/shared/constants/input.constants';
import { SERVER_API_URL } from 'app/app.constants';
import { createRequestOption } from 'app/shared';
import { IExtendedUser } from 'app/shared/model/extended-user.model';
type EntityResponseType = HttpResponse<IExtendedUser>;
type EntityArrayResponseType = HttpResponse<IExtendedUser[]>;
@Injectable({ providedIn: 'root' })
export class ExtendedUserService {
private resourceUrl = SERVER_API_URL + 'api/extended-users';
constructor(private http: HttpClient) {}
create(extendedUser: IExtendedUser): Observable<EntityResponseType> {
const copy = this.convertDateFromClient(extendedUser);
return this.http
.post<IExtendedUser>(this.resourceUrl, copy, { observe: 'response' })
.map((res: EntityResponseType) => this.convertDateFromServer(res));
}
update(extendedUser: IExtendedUser): Observable<EntityResponseType> {
const copy = this.convertDateFromClient(extendedUser);
return this.http
.put<IExtendedUser>(this.resourceUrl, copy, { observe: 'response' })
.map((res: EntityResponseType) => this.convertDateFromServer(res));
}
find(id: number): Observable<EntityResponseType> {
return this.http
.get<IExtendedUser>(`${this.resourceUrl}/${id}`, { observe: 'response' })
.map((res: EntityResponseType) => this.convertDateFromServer(res));
}
query(req?: any): Observable<EntityArrayResponseType> {
const options = createRequestOption(req);
return this.http
.get<IExtendedUser[]>(this.resourceUrl, { params: options, observe: 'response' })
.map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res));
}
delete(id: number): Observable<HttpResponse<any>> {
return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response' });
}
private convertDateFromClient(extendedUser: IExtendedUser): IExtendedUser {
const copy: IExtendedUser = Object.assign({}, extendedUser, {
fechaIngreso:
extendedUser.fechaIngreso != null && extendedUser.fechaIngreso.isValid()
? extendedUser.fechaIngreso.format(DATE_FORMAT)
: null
});
return copy;
}
private convertDateFromServer(res: EntityResponseType): EntityResponseType {
res.body.fechaIngreso = res.body.fechaIngreso != null ? moment(res.body.fechaIngreso) : null;
return res;
}
private convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
res.body.forEach((extendedUser: IExtendedUser) => {
extendedUser.fechaIngreso = extendedUser.fechaIngreso != null ? moment(extendedUser.fechaIngreso) : null;
});
return res;
}
}
出现错误时用户服务中的代码
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { SERVER_API_URL } from 'app/app.constants';
import { createRequestOption } from 'app/shared/util/request-util';
import {IUser } from './user.model';
import {ExtendedUser, IExtendedUser} from "../../shared/model/extended-user.model";
import {DATE_FORMAT} from "../../shared";
import * as moment from 'moment';
type EntityResponseType = HttpResponse<IExtendedUser>;
@Injectable({ providedIn: 'root' })
export class UserService {
private resourceUrl = SERVER_API_URL + 'api/users';
constructor(private http: HttpClient) {}
create(user: IUser): Observable<HttpResponse<IUser>> {
const copy = this.convertDateFromClient(user);
return this.http.post<IUser>(this.resourceUrl + '-and-extendedUser', copy, { observe: 'response' })
.map((res: EntityResponseType) => this.convertDateFromServer(res));
}
update(user: IUser): Observable<HttpResponse<IUser>> {
const copy = this.convertDateFromClient(user);
return this.http.put<IUser>(this.resourceUrl, copy , { observe: 'response' })
.map((res: EntityResponseType) => this.convertDateFromServer(res));
}
find(login: string): Observable<HttpResponse<IUser>> {
return this.http.get<IExtendedUser>(`${this.resourceUrl}/${login}`, { observe: 'response' });
}
query(req?: any): Observable<HttpResponse<IUser[]>> {
const options = createRequestOption(req);
return this.http.get<IUser[]>(this.resourceUrl, { params: options, observe: 'response' }) ;
}
delete(login: string): Observable<HttpResponse<any>> {
return this.http.delete(`${this.resourceUrl}/${login}`, { observe: 'response' });
}
authorities(): Observable<string[]> {
return this.http.get<string[]>(SERVER_API_URL + 'api/users/authorities');
}
private convertDateFromClient(user: IExtendedUser): IUser {
const copy: IUser = Object.assign({}, user, {
fechaIngreso:
user.fechaIngreso != null && user.fechaIngreso.isValid()
? user.fechaIngreso.format(DATE_FORMAT)
: null
});
return copy;
}
private convertDateFromServer(res: EntityResponseType): EntityResponseType {
res.body.fechaIngreso = res.body.fechaIngreso != null ? moment(res.body.fechaIngreso) : null;
return res;
}
}
我想在日期选择器中显示日期。
答案 0 :(得分:0)
更新
我发现了问题
我没有将日期设置为:
find(login: string): Observable<HttpResponse<IUser>> {
return this.http.get<IExtendedUser>(`${this.resourceUrl}/${login}`, { observe: 'response' });
}
所以我添加了它
find(login: string): Observable<HttpResponse<IUser>> {
return this.http.get<IExtendedUser>(`${this.resourceUrl}/${login}`, { observe: 'response' })
.map((res: EntityResponseType) => this.convertDateFromServer(res));
}