Javascript 无法识别导出类中的输入值,导致将其呈现为未定义的错误
我在Ionic Framework 6.13.1上,我有两个具有相同代码结构的服务。我有一个Javascript 无法识别导出类中的输入值,导致将其呈现为未定义的错误,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我在Ionic Framework 6.13.1上,我有两个具有相同代码结构的服务。我有一个预约.service.ts和一个注册.service.ts。这些服务中的每一个都引用一个单独的Typescript文件,其中只包含一个用于存储值的导出类,registration.service.ts的类位于registration.ts中,appointment.service.ts的类位于appointment.ts中。以下是上述每个文件的代码: 预约服务.ts: 注册.service.ts: 在我的
预约.service.ts
和一个注册.service.ts
。这些服务中的每一个都引用一个单独的Typescript文件,其中只包含一个用于存储值的导出类,registration.service.ts
的类位于registration.ts
中,appointment.service.ts
的类位于appointment.ts
中。以下是上述每个文件的代码:
预约服务.ts
:
注册.service.ts
:
在我的make appointment.page.ts
文件中引用这两项服务时,当用户键入他们的输入时,我没有问题,但在我的registration.page.ts
文件中,一旦用户提交输入,控制台就会出现错误,说明:
错误类型错误:无法读取未定义的属性“push”
为什么我的输入在我的预约
页面中被识别,而在我的注册
页面中被识别
非常感谢您的帮助。正如@Ravikumar提到的,您的bookingListRef似乎没有初始化 在构建服务时,可以通过检索列表对其进行修改。只需在构造函数中调用
callBookingList()
,例如:
构造函数(私有数据库:AngularFireDatabase){
这是getBookingList()
}
我能想到的唯一问题是,您刚刚声明了bookingListRef
,但没有使用至少一个空数组初始化,如bookingListRef:AngularFireList=[]
。除非您在make appoint.page.ts中提供您正在做的事情,否则很难猜出问题所在。谢谢@Ravikumar!问题在于初始化。使用@Anders的方法后,注册页面现在可以工作了。谢谢!这就是问题所在。我将这个.getBookingList()添加到我的registration.service.ts中的构造函数中,现在用户从注册页面的输入被存储在Firebase中。
import { Injectable } from '@angular/core';
import { Appointment } from '../shared/Appointment';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from '@angular/fire/database';
@Injectable({
providedIn: 'root'
})
export class AppointmentService {
bookingListRef: AngularFireList<any>;
bookingRef: AngularFireObject<any>;
constructor(private db: AngularFireDatabase) { }
// Create
createBooking(apt: Appointment) {
return this.bookingListRef.push({
name: apt.name,
email: apt.email,
mobile: apt.mobile,
car: apt.car,
image1: apt.image1,
minPrice: apt.minPrice,
maxPrice: apt.maxPrice,
steps: apt.steps
})
}
// Get Single
getBooking(id: string) {
this.bookingRef = this.db.object('/appointment/' + id);
return this.bookingRef;
}
// Get List
getBookingList() {
this.bookingListRef = this.db.list('/appointment');
return this.bookingListRef;
}
// Update
updateBooking(id, apt: Appointment) {
return this.bookingRef.update({
name: apt.name,
email: apt.email,
mobile: apt.mobile,
car: apt.car,
image1: apt.image1,
minPrice: apt.minPrice,
maxPrice: apt.maxPrice,
steps: apt.steps
})
}
// Delete
deleteBooking(id: string) {
this.bookingRef = this.db.object('/appointment/' + id);
this.bookingRef.remove();
}
}
export class Appointment {
$key: string;
name: string;
email: string
mobile: number;
car: string;
image1: string;
minPrice: number;
maxPrice: number;
currentPrice: number;
steps: number;
}
import { Injectable } from '@angular/core';
import { Registration } from '../shared/Registration';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from '@angular/fire/database';
@Injectable({
providedIn: 'root'
})
export class RegistrationService {
bookingListRef: AngularFireList<any>;
bookingRef: AngularFireObject<any>;
constructor(private db: AngularFireDatabase) { }
// Create
createBooking(apt: Registration) {
return this.bookingListRef.push({
name: apt.name,
mobile: apt.mobile,
email: apt.email,
confirmEmail: apt.confirmEmail
})
}
// Get Single
getBooking(id: string) {
this.bookingRef = this.db.object('/Registration/' + id);
return this.bookingRef;
}
// Get List
getBookingList() {
this.bookingListRef = this.db.list('/Registration');
return this.bookingListRef;
}
// Update
updateBooking(id, apt: Registration) {
return this.bookingRef.update({
name: apt.name,
mobile: apt.mobile,
email: apt.email,
confirmEmail: apt.confirmEmail
})
}
// Delete
deleteBooking(id: string) {
this.bookingRef = this.db.object('/Registration/' + id);
this.bookingRef.remove();
}
}
export class Registration {
$key: string;
name: string;
mobile: number;
email: string;
confirmEmail: string;
}