0

The whole function allPrototypes(userModel, callback) gets called for every userModel in the DB, let's say 100 times.

My Problem: Because of the asynchronous JS the function addJobToUser(userId, jobId) is called only with the last of the 100 userModels. So addJobToUser is called 100 times but not with userModel 1 to 100, but with userModel 100 every single time (as you can see on my comments). I can imagine that the problem is the asynchronous JS.

Do you guys have a solution for my problem?

function allPrototypes(userModel, callback){
    // HERE console.log() -> USER MODELS 1-100 
    var MongoClient = mongo.MongoClient;
    var url = 'mongodb://localhost:27017/uniyapp';

    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        var cursor = db.collection('jobprototypes').find();
        cursor.each(function (err, doc) {
            if(doc != null){
                console.log(userModel.id);
                // create prototypeModel
                var jobPrototypeModel = new prototype.emptyPrototype();
                jobPrototypeModel.attributes = doc['attributes'];
                jobPrototypeModel.contributors = doc['contributors'];
                jobPrototypeModel.id = doc['_id'];
                // now calculate the similarity between prototypemodel and usermodel
                // HERE console.log() -> USER MODELS 1-100 
                console.log(callback(jobPrototypeModel, userModel));

                if(callback(jobPrototypeModel, userModel) <= threshold){
                    addJobToUser(userModel.id, jobPrototypeModel.id);
                }
            }
        });
        db.close();
    });
}

function addJobToUser(userId, jobId){
    var MongoClient = mongo.MongoClient;
    var url = 'mongodb://localhost:27017/uniyapp';

    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        db.collection('jobusers').update({_id:userId}, {'$set' : {jobs: ['nevermind']}});
        db.close();
    });
}
Vishnu
  • 11,614
  • 6
  • 51
  • 90
philszalay
  • 423
  • 1
  • 3
  • 9
  • Possible duplicate of [javascript infamous loop issue](http://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue) – Gangadhar Jannu Mar 21 '17 at 14:06
  • You pass a singe `userModel` to `allPrototypes()`. Where do you expect the other 99 userModels to come from? This function is just forwarding the userModel **you** provide. – Thomas Mar 21 '17 at 14:39
  • @Thomas allPrototypes() is called 100 times with 100 different userModels. – philszalay Mar 21 '17 at 14:41
  • Are you sure that they are different? You might want to include that code too, since it seems to cause your problem. – Thomas Mar 21 '17 at 14:43
  • @GangadharJannu thank you very much, that solved my problem !! also thanks to Thomas for your help! – philszalay Mar 21 '17 at 15:37
  • Possible duplicate of [Javascript infamous Loop issue?](http://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue) – ford prefect Mar 21 '17 at 21:24

0 Answers0