Angular 无法在typescript中设置未定义的属性(已定义)

Angular 无法在typescript中设置未定义的属性(已定义),angular,typescript,ionic-framework,google-cloud-firestore,Angular,Typescript,Ionic Framework,Google Cloud Firestore,这个问题可能与async/await有关,但我尝试将它们放在函数中的几个位置 //this is at the top of the class userFriends: any = []; matches() { var matches = []; this.isVisible2 = true; this.afstore.collection("matches").valueChanges

这个问题可能与async/await有关,但我尝试将它们放在函数中的几个位置

    //this is at the top of the class
    userFriends: any = [];

    matches()
    {
        var matches = [];

        this.isVisible2 = true;

        this.afstore.collection("matches").valueChanges().pipe(take(1)).subscribe((val: any) => {
            this.userMatches = [];
            matches = [];


            // this part up here is a bit ugly and i could use functions inside if statements but first i wanted to get the code working, then take care of other things

            for(var i=0;i<val.length;i++)
            {
                if(val[i].user1 === this.user.getUID())
                {
                    this.userMatches.push({
                        id: val[i].id,
                        likedMovies: val[i].likedMovies,
                        maxTurns: val[i].maxTurns,
                        ratedMovies: val[i].ratedMovies,
                        turn: val[i].turn,
                        turnNumber: val[i].turnNumber,
                        me: val[i].user1,
                        friend: val[i].user2
                    });
                }
                else if(val[i].user2 === this.user.getUID())
                {
                    this.userMatches.push({
                        id: val[i].id,
                        likedMovies: val[i].likedMovies,
                        maxTurns: val[i].maxTurns,
                        ratedMovies: val[i].ratedMovies,
                        turn: val[i].turn,
                        turnNumber: val[i].turnNumber,
                        me: val[i].user2,
                        friend: val[i].user1
                    });
                }
            }

            if(this.userMatches.length === 0)
            {
                this.isVisible2 = false;
            }
            else
            {
                //the console log here works just fine like it should

                console.log(this.userMatches);

                for(var i=0; i<this.userMatches.length;i++)
                {

                    // logging here works fine aswell
                    console.log(this.userMatches[i]);

                    this.afstore.collection("users").doc(this.userMatches[i].friend).valueChanges().pipe(take(1)).subscribe((val2: any) => {
                        console.log(this.userMatches[i]);
                        this.userMatches[i].friend = val2.nickname;
                        //matches.push(val2.nickname);
                    });
                }

                //it also logs perfectly fine here, also it would log fine if i put it in a for and logged each object in the array individually
                console.log(this.userMatches);

            }
        });
    }
//这是类的顶部
userFriends:any=[];
匹配项()
{
var匹配=[];
this.isVisible2=真;
this.afstore.collection(“matches”).valueChanges().pipe(take(1)).subscribe((val:any)=>{
this.userMatches=[];
匹配项=[];
//这部分有点难看,我可以在if语句中使用函数,但首先我想让代码正常工作,然后再处理其他事情

对于(var i=0;i
this.userMatches[i]
在您开始在传递给
doc()
的参数中使用它之前没有明确定义,仅从您在此处显示的内容判断。在运行任何代码之前记录它的值,以验证JavaScript所说的内容(在这个问题上不会出错):


我添加了完整的函数和注释,解释它在何处记录良好以及何时记录不正确。您没有记录正确的内容。投诉是关于
this.userMatches[i]
-就在
friend
的属性访问之前。由于我们看不到您的数据以及填充
userMatches
的循环的执行方式,因此我们无法提供太多帮助。您需要进行一些调试,以找出在此处使用该数组时该数组的元素
i
未定义的原因。我添加了一个de>console.log(this.userMatches[i])
在for循环中工作正常。它在第二个firestore函数中停止工作。我还添加了一个浏览器图像,显示控制台日志的输出。我相信我已经解决了问题,在firestore函数中我没有定义
console.log(this.userMatches[i])