Angular 无法读取ionic 2中未定义的属性$key
我试图在ionic2应用程序中更新firebase中的数据,但出现以下错误:Angular 无法读取ionic 2中未定义的属性$key,angular,firebase,firebase-realtime-database,ionic2,Angular,Firebase,Firebase Realtime Database,Ionic2,我试图在ionic2应用程序中更新firebase中的数据,但出现以下错误: 无法读取未定义的属性“$key” .ts .html <button ion-button type="submit" block [disabled]="!f.valid" (click)="onValidate(info)">Valider</button> Valider 在我的html中,我有一个*ngFor=“let info of infos | async”
无法读取未定义的属性“$key”
.ts
.html
<button
ion-button
type="submit"
block
[disabled]="!f.valid"
(click)="onValidate(info)">Valider</button>
Valider
在我的html中,我有一个*ngFor=“let info of infos | async”
谢谢您的帮助我想您正在使用firebase。您永远不会从angular
ngFor
获得$key
,因为它不属于结构数组,因为$key
是一个标识符,而不是firebase数据结构中的属性
您能做什么当您第一次得到它时,将$key
推到阵列上。虽然您没有显示如何获得infos
,但我认为它可能是这样的
this.api.get(`mydatabase/infos`).then(infosData => {
this.infos = infodata;
});
在返回的promise中,您可以访问$key
,然后可以将其推入视图中使用的数组中
this.api.get(`mydatabase/infos`).then(infosData => {
infosData.forEach((el,idx) => {
console.log(el.$key);
// Use the index as you should be pushing onto an object literal
// Of course this could be different depending how you have
// structured the data being returned for firebase which is not
// specified in your question
infos[idx].push('key') = el.$key;
});
});
然后,视图中用于
ngFor
的数组现在将有一个属性info.key
,您可以在onValidate(info)
方法中使用该属性作为标识符。我遇到了类似的问题,后来我查看了提供的示例代码。键值从快照更改()中获取。因此,在这里,我使用JSX spread属性以以下方式存储每个子级的键值
从'@angular/core'导入{Component};
从“angularfire2/database”导入{AngularFireDatabase,AngularFireList};
从“rxjs/Observable”导入{Observable};
导入'rxjs/add/operator/map';
@组成部分({
选择器:'应用程序根',
模板:`
-
更新
删除
添加
全部删除
`,
})
导出类AppComponent{
itemsRef:角度火灾列表;
项目:可观察;
构造函数(db:AngularFireDatabase){
this.itemsRef=db.list('messages');
//使用snapshotChanges().map()存储密钥
this.items=this.itemsRef.snapshotChanges().map(更改=>{
返回changes.map(c=>({key:c.payload.key,…c.payload.val()}));
});
}
您的问题中没有足够的代码来确定。大概,infos
是一个承诺
或可观察的
正在解决或推动未定义的
infos
是infos:FirebaseListObservable
,您需要其他信息吗?@Aluan HaddadYes,未定义
可分配给any
。在此上下文中使用any
基本上可以抑制错误映射调用(缺少返回或参数)可能导致的任何类型错误,意外地将一个空函数映射到一个可观察对象上,以及大量其他错误。不要使用任何
,除非在极少数情况下使用逗号,因为您显然对对象有一个预期的形状。无论如何,调试它的最简单方法是订阅viewmodel中的可观察对象并简单地记录每个值hanks@AluanHaddad.我不知道如何记录每个值?在设置this.infos=which;
addthis.infos.subscribe(console.log)
谢谢!!@gerdi
this.api.get(`mydatabase/infos`).then(infosData => {
infosData.forEach((el,idx) => {
console.log(el.$key);
// Use the index as you should be pushing onto an object literal
// Of course this could be different depending how you have
// structured the data being returned for firebase which is not
// specified in your question
infos[idx].push('key') = el.$key;
});
});