【发布时间】:2019-01-22 12:34:10
【问题描述】:
我是打字稿的新手。我试过在这里搜索答案,但似乎没有一个解决方案适合。
我有一个相对简单(我猜)的问题。 我使用profilesArr 数组获得的数据是正确的,但我似乎无法将该数据传递给我在课程开始时声明的数组“profiles”。但是,它确实将数据从profilesArr 正确传递到本地声明的名为“array”的数组。我可以轻松地从“数组”中获取数据,但是当我运行这行代码时 this.profiles[i]=profilesArr[k];它给了我错误:无法读取 null 的属性“配置文件”。
export class RegisteredusersPage {
private profileListRef = this.db.list<Profile>('profile-list');
constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage,
private db: AngularFireDatabase) {
}
profiles=[];
ionViewDidLoad() {
console.log('ionViewDidLoad RegisteredusersPage');
}
getProfile(){
console.log(this.db.list('profile-list'));
var database = firebase.database();
var ref=database.ref("profile-list");
ref.on('value', this.gotData, this.errData)
}
gotData(data){
console.log(data.val());
var profilesArr=data.val();
var keys=Object.keys(profilesArr);
var that=this;
var array=[];
console.log("Pre keys");
console.log("keys:"+keys);
console.log("profilesArr: "+profilesArr);
for(var i = 0; i <keys.length; i++){
var k = keys[i];
var profileName = profilesArr[k].name;
var profileCountry = profilesArr[k].country;
console.log(profileName,profileCountry);
alert(profileName);
alert(profileCountry);
array[i]=profilesArr[k];
this.profiles[i]=profilesArr[k];
}
console.log(this.profiles);
}
errData(err){
console.log("Error");
console.log(err);
}
我知道这是一个相当新手的问题,但我无法理解它。如果您能帮我解决这个问题,我将真的感激不尽,谢谢!
【问题讨论】:
-
您提到了其他答案,但没有说明为什么它们不适用。为了帮助人们不仅仅是给你其他答案,也许告诉我们为什么这些解决方案不适合?
-
感谢您的回答!关于答案,我已经阅读了它们,但我并没有真正弄清楚“这个”。这对我来说很难,我来自 Java,而且看起来很不一样。我已经读过@MattMcCutchen,但我仍然不确定我应该在我的代码中进行哪些更改才能使其正常工作。很抱歉给您添麻烦,但我做不到。
-
是的,
this在 JavaScript 中很痛苦。我添加了一些代码示例;希望我至少有一个是正确的。
标签: javascript typescript ionic2