asp.net - 未捕获的错误:无法将“未定义”转换为日期(角度)
问题描述
在此示例中,我想从服务中获取所有预订数据并将它们推送到数组 CalendarEvent[]。但我收到了这个错误Uncaught Error: Unable to convert "undefined" into a date
。我认为我的服务有问题?
ng-calendar.component.ts:
export class NgCalendarComponent {
events: CalendarEvent[] = [];
bookings: any = {};
date: string;
constructor(private bookingService: BookingService) {}
ngOnInit() {
this.bookings = this.bookingService.getAllBookings();
for (let booking of this.bookings) {
var b = this.bookings[booking];
this.date = formatDate(b.bookDate, 'medium', 'en-us', 'GMT+8');
this.events.push({
start: new Date(this.date),
title: b.purpose,
});
}
}
预订.service.ts:
export class BookingService {
constructor(private http: HttpClient) { }
getAllBookings() {
return this.http.get('/api/bookings/')
.pipe(map(response => response));
}
}
BookingResource.cs:
public class BookingResource
{
public int Id { get; set; }
public RoomResource Room { get; set; }
public KeyValuePairResource Building { get; set; }
public DateTime BookDate { get; set; }
public ContactResource Contact { get; set; }
public string Purpose { get; set; }
public ICollection<TimeSlotResource> TimeSlots { get; set; }
public BookingResource()
{
TimeSlots = new Collection<TimeSlotResource>();
}
}
解决方案
getAllBooking()
返回一个 observable,你必须订阅它才能得到结果。或将其转换为承诺:
async getAllBookings() {
return this.http.get('/api/bookings/').toPromise();
}
并这样称呼它:
async ngOnInit() {
this.bookings = await this.bookingService.getAllBookings();
...
推荐阅读
- python - 是否可以有一个一致的 setup.py 文件来反映对依赖项的本地更改?
- python - 如何根据二进制掩码删除numpy中的列?
- java - 在 Java 中打印前 1000 个 Unicode 字符
- python - Orator ORM模型创建方法无效SQL
- java - 如何在 IntelliJ 中配置默认项目构建
- php - .htaccess - 将前缀字符串添加到 URL 的开头
- node.js - 将大文件上传到 EB AWS 服务失败并出现错误 400 bad request
- c# - .Net 设置和获取 Dictionary 属性
- java - 如何将 LocalDate 格式化为用户位置的标准格式?
- wso2 - WSO2 IS 5.3.0 - 租户的管理员用户可以被锁定/禁用吗?