无法设置打字稿中未定义的属性(已定义)

时间:2020-09-21 19:31:25

标签: 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().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);

            }
        });
    }

我得到的错误是ERROR TypeError: Cannot set property 'friend' of undefined,但是如果明确定义了错误,我怎么能得到呢?控制台日志是正确的,这意味着this.userMatches[i].friend在使用它作为文档名称时不是未定义的,但是在我尝试再次设置它时它是未定义的。

这是console.log结果的图像 前三个对象是我在for中执行console.log(this.userFriends[i])时的最后一个对象,而最后一个3x undefined是在firestore函数中 console.log results

1 个答案:

答案 0 :(得分:1)

this.userMatches[i] 之前没有明确定义,您只能在传递给doc()的参数中使用它,仅从此处显示的内容来看。在运行任何代码之前,请先记录其值,以验证JavaScript在说什么(在这个问题上这不会错):

console.log(this.userMatches[i])