1

I have this function, for example:

app.get('/', function(req, res) {
    var token = req.query.token;
    if(!token) {
        res.render('auth'); // Authentication
    } else {
        authorizedUsers.forEach(function(user) {
            if(user.id == token) {
                console.log('found, nickname: ' + user.nickname);
                return true;
            }
        });
        console.log('not found');
        return false;
    }
});

Basically it's looping through the authorizedUsers array, and looking for an entry where entry.id equals the token variable.

What I wanted to do is, if found, to return true and stop the execution of the rest of the app.get('/')... block.

If not found, obviously the forEach loop has already gone through all entries, and eventually the execution arrives to the printing of "not found" and return false;.

For my current code, even though an entry is found, execution continues and I still get the 'not found' log. enter image description here

Am I missing anything?

To simplify things, what I wanna do is:

  1. Looping through all authorizedUsers entries, comparing entry.id to the token variable.
  2. If found, print "found" to the console and stop the execution.
  3. If not found, print "not found" to the console and stop the execution.

Thank you.

Edit

Following Michael's solution, this is my working code:

app.get('/', function(req, res) {
    var token = req.query.token;
    if(!token) {
        res.render('auth'); // Authentication
    } else {
        if(!authorizedUsers.some(function(user) {
            if(user.id == token)
                return true;
        })) {
            console.log('No entries found.');
        } else {
            console.log('Entries found!');
        }
    }
});
baao
  • 71,625
  • 17
  • 143
  • 203
Ariel Weinberger
  • 2,195
  • 4
  • 23
  • 49

2 Answers2

4

You would use Array.prototype.some for this:

authorizedUsers.some(function(user) {
    return user.id == token;
}

The some() method tests whether some element in the array passes the test implemented by the provided function.

baao
  • 71,625
  • 17
  • 143
  • 203
  • 1
    Thanks, I will set this as answer. I have also edited my own question with the solution using .some(). Thanks a lot @Michael! That was super helpful. – Ariel Weinberger Feb 16 '16 at 01:08
0

The forEach function cannot be interrupted unless the code inside throws an exception.

So you could either do something like this

Or just let it run through all the records doing something like this:

   var found = false;
   authorizedUsers.forEach(function(user) {
        if(user.id == token) found = true;
    });
    console.log('found? ', found);
Community
  • 1
  • 1
nbermudezs
  • 2,814
  • 1
  • 20
  • 20
  • Viola! I thought something was wrong with me, because I used to work with PHP Laravel for a long time and it worked like a charm the way I did it. I knew something is wrong. I am pretty new to Node. Thanks for your advise! – Ariel Weinberger Feb 16 '16 at 01:03