最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'Symbol(Symbol.iterator)' of undefined - Stack Overflow

programmeradmin2浏览0评论

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.prototype.any = (comparator) => {
    for(let item of this){
        if(comparator(item)){
            return true;
        }
    }
    return false;
};

Then calling Array.any() like:

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.

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

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.prototype.any = (comparator) => {
    for(let item of this){
        if(comparator(item)){
            return true;
        }
    }
    return false;
};

Then calling Array.any() like:

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.

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

Share Improve this question edited Jun 2, 2017 at 13:34 Bergi 665k160 gold badges1k silver badges1.5k bronze badges asked Jun 2, 2017 at 12:45 Ids van der ZeeIds van der Zee 8821 gold badge13 silver badges24 bronze badges 12
  • 1 What is "users"? It would appear not to be an array but is rather a Query object from mongoose. I think you did something like var users = User.find({}). Yes? – Neil Lunn Commented Jun 2, 2017 at 12:48
  • 2 user.userName === user.userName: do you really mean that? – PeterMader Commented Jun 2, 2017 at 12:49
  • You could be correct here "users" is indeed comming from mongoose – Ids van der Zee Commented Jun 2, 2017 at 12:49
  • 3 Also, can't you just use Array.prototype.some()? – PeterMader Commented Jun 2, 2017 at 12:50
  • 1 @IdsvanderZee Please add that as an answer then, not by editing your question - which completely invalidated the error message. – Bergi Commented Jun 2, 2017 at 13:35
 |  Show 7 more comments

2 Answers 2

Reset to default 5

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".

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:

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);
        }
    });
};
发布评论

评论列表(0)

  1. 暂无评论