title: Illegal break statement (Node.js)
body: <p>Trying to find unique ID in Node.js and MongoDB, by creating a while loop that queries MongoDB for existing IDs, until a unique value is found. If the ID is already in use, a number is incremented on the end until Mongo returns nothing.</p>
  
  <p>Everything is working, except for the <code>break;</code> statement when a unique ID is found. Node.js returns: <code>SyntaxError: Illegal break statement</code></p>
  
  <p>The code:</p>
  
  <pre><code>db.collection('landmarks').findOne({'id':uniqueIDer}, function(err, data){
      //if ID exists already
      if (data.id){
  
          var uniqueNumber = 1;
  
           while (1) {
  
              var uniqueNum_string = uniqueNumber.toString(); 
              var newUnique = data.id + uniqueNum_string;
              db.collection('landmarks').findOne({'id':newUnique}, function(err, data){
  
                  if (data.id){
                      uniqueNumber++;
                  }
  
                  else {
                      saveLandmark(newUnique);
                      break;
                  }
              });
          }
      }
  
      else {
          saveLandmark(uniqueIDer);
      }
  
  });
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>EDIT:</p>
  
  <p>Here's the fixed up code using async if anyone needs it :)</p>
  
  <pre><code>        db.collection('landmarks').findOne({'id':uniqueIDer}, function(err, data){
  
              if (data){
                  var uniqueNumber = 1;
                  var newUnique;
  
                  async.forever(function (next) {
                    var uniqueNum_string = uniqueNumber.toString(); 
                    newUnique = data.id + uniqueNum_string;
  
                    db.collection('landmarks').findOne({'id':newUnique,'world':worldVal}, function(err, data){
                      if (data){
                        console.log('entry found!');
                        uniqueNumber++;
                        next();
                      }
                      else {
                        console.log('entry not found!');
                        next('unique!'); // This is where the looping is stopped
                      }
                    });
                  },
                  function () {
                    saveLandmark(newUnique);
                  });
              }
              else {
                  saveLandmark(uniqueIDer);
              }
          });
  </code></pre>