Your break statement is not inside the loop body. Instead, it is inside the body of the function, namely the findOne . To make this clearer, it is useful to temporarily use a named function as a callback handler:
var cb = function(err, data){ if (data.id){ uniqueNumber++; } else { saveLandmark(newUnique); break; // not inside a loop! } }; 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}, cb); } } else { saveLandmark(uniqueIDer); } });
Now it’s clear that break in the body of the callback function is not inside the loop! I also violated other things because the values of uniqueNumber and newUnique no longer in scope, but this is a different problem. :) It is important to note that the function introduces a “hard” border in your code, which is difficult to understand based solely on the language syntax. This is one of the reasons why this programming callback style can be so complex as to be eligible.
Actually, it’s much harder to do this than your original code attempt would have expected. You will need to pass a success signal through possibly arbitrary callback levels, as you repeatedly call findOne and parse the result (asynchronously).
You can get some help with this using the excellent async library like https://github.com/caolan/async#whilst .
lmjohns3
source share