【问题标题】:Cannot set property of undefined in typescript (it is defined)无法在打字稿中设置未定义的属性(已定义)
【发布时间】:2021-01-07 22:27:00
【问题描述】:

这个问题可能与 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 函数内部

【问题讨论】:

    标签: angular typescript ionic-framework google-cloud-firestore


    【解决方案1】:

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

    console.log(this.userMatches[i])
    

    【讨论】:

    • 我已经添加了完整的函数和 cmets 来解释它在哪里可以正常记录以及何时不能正常记录
    • 您记录的内容不正确。投诉是关于this.userMatches[i] - 就在friend 的财产访问之前的事情。由于我们看不到您的数据以及填充 userMatches 的循环如何执行,因此我们无能为力。您需要进行一些调试来弄清楚为什么在此处使用该数组的元素 i 时未定义它。
    • 我在 for 循环中添加了一个console.log(this.userMatches[i]),它工作正常。它停止在第二个 Firestore 功能内工作。我还添加了显示控制台日志输出的浏览器图像
    • 我相信我已经解决了问题,在 firestore 函数内部我是未定义的
    猜你喜欢
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2021-11-27
    相关资源
    最近更新 更多