我正试图将用户输入中的数据添加到firebase中,但它不起作用。这是我的输入字段:
<div class="bed-price cc">
<label for="name" class="subtitle-secondary">Name</label>
<input *ngIf="pePropertyStep" type="text" name="name" class="minimal_input shadow_xlight"
[(ngModel)]="pePropertyStep.mid_tenants.mid_tenant_name" #name="ngModel" placeholder="Name">
</div>
这就是型号:
export interface PropertyPropertyModel {
description: string;
lease_type: string;
is_mid_tenancy: boolean;
mid_tenants: Array<{ mid_tenant_name: string, mid_tenant_email: string, mid_tenant_rent_price: number, status: string}>;
}
以及我如何初始化它:
initPropertyProperty = (): PropertyPropertyModel => {
return {
description: '',
is_mid_tenancy: false,
mid_tenants: [
{ mid_tenant_name: '', mid_tenant_email: '', mid_tenant_rent_price: 0, status: 'pending'}
],
}
并填充它:
populatePropertyProperty = (property: PropertyModel): PropertyPropertyModel => {
return {
description: property.description,
is_mid_tenancy: property.is_mid_tenancy,
mid_tenants: property.mid_tenants,
}
然而,在我尝试并推送到firebase之前,我在控制台中收到了这个错误:
ERROR TypeError: Cannot read property 'mid_tenant_name' of undefined
at Object.eval [as updateDirectives] (PeInfoPropertyComponent.html:19)
这就是上面的html部分。我已经用这种确切的方法上传到firebase的其他形式,它已经完美地工作了。这与我正在创建一个对象数组的事实有关吗?
这是因为属性mid_tenants
在模型中被定义为数组,而您对它的访问不正确指定阵列位置以访问对象示例
[(ngModel)]="pePropertyStep.mid_tenants[0].mid_tenant_name"