Javascript 无法读取未定义的属性 'Symbol(Symbol.iterator)'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44329324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:27:38  来源:igfitidea点击:

Cannot read property 'Symbol(Symbol.iterator)' of undefined

javascriptarraysnode.jsmongoose

提问by Ids van der Zee

I'm trying to loop through an array to check if it contains any item that passes a specified function. I do this by adding a .any() prototype to the Array object:

我正在尝试遍历一个数组以检查它是否包含任何通过指定函数的项目。我通过向 Array 对象添加一个 .any() 原型来做到这一点:

Array.prototype.any = (comparator) => {
    for(let item of this){
        if(comparator(item)){
            return true;
        }
    }
    return false;
};

Then calling Array.any() like:

然后调用 Array.any() 像:

else if(users && users.any((user) => user.userName === user.userName)){
        res.status(400).send('Username already in use');
}

This however gives me the following error:

然而,这给了我以下错误:

TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
at Array.any (C:\dev\nodejs\testproject\dist\routes\userRoutes.js:29:39)
at C:\dev\nodejs\testproject\dist\routes\userRoutes.js:87:56
at Query.<anonymous> (C:\dev\nodejs\testproject\node_modules\mongoose\lib\model.js:3748:16)
at C:\dev\nodejs\testproject\node_modules\kareem\index.js:277:21
at C:\dev\nodejs\testproject\node_modules\kareem\index.js:131:16
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

The error seems to me like it is suggesting 'this' in the prototype function is undefined, but 'this' is the users array for which i checked for undefined.

在我看来,该错误似乎暗示原型函数中的“this”未定义,但“this”是我检查未定义的用户数组。

Not realy sure what is actually causing the issue, hope you can help.

不太确定是什么导致了这个问题,希望你能帮忙。

采纳答案by Shl

The only answer here is that you didn't use "function", so your "this" is not your "users". This would work:

这里唯一的答案是你没有使用“功能”,所以你的“这个”不是你的“用户”。这会起作用:

Array.prototype.any = function(comparator) {
    for(let item of this){
        if(comparator(item)){
            return true;
        }
    }
    return false;
};

And then, of course, just use "some".

然后,当然,只需使用“一些”。

回答by Ids van der Zee

Using Array.prototype.any() was unnecesary as I was using mongoose to get the users so I changed it to have mongoose try to get a single user that has the secified username and checking if that was defined. Like:

使用 Array.prototype.any() 是不必要的,因为我使用 mongoose 来获取用户,所以我将其更改为让 mongoose 尝试获取具有特定用户名的单个用户并检查是否已定义。喜欢:

const checkUniqueUserName = (user, res, done) => {
    User.findOne({"userName": user.userName}, (err, foundUser) => {
        if(err){
            res.sendStatus(500);
            console.log(err);
        }

        else if(foundUser){
            res.status(400).send('Username already in use');
        }

        else{
            done(user);
        }
    });
};