简体   繁体   中英

Asynchronous Node, Mongoose Inserting in Data

I have a CSV file that I am reading in with hundreds of lines. Based of a row in the CSV file I am creating a 'project' and storing it into my projects collection. Each project has a Category as well which is its own Model. So many projects can have the same Category. Both collections start of with nothing and I need to populate both these collections based of the CSV file. The issue I am having is that since many rows contain the same category, often right after another, multiple category documents of the same type are created since everything is happening asynchronously. I have code like this:

    exports.upload = function(req, res) {
var reader = csv.createCsvFileReader("test.csv", {columnsFromHeader:true, nestedQuotes:true});
reader.addListener('data', function(data) {        
    projectService.findCategory(data.Category, function(id){
        var Project = mongoose.model('Project');
        var project = new Project();
        project.name = data.Name;
        project.narrative = data.Narrative;
        project.address = data.address;
        project.category = id;
        project.lat = data.Lat;
        project.lng = data.Lng;
        project.save(function(err){
            console.log(project);
            if(err){
                console.log("There was an error in saving your project");
                console.log(err);
                return;
            }
        });
    });
});

}

and

    exports.findCategory = function(projectCategory, callback) {
var Category = mongoose.model('Category');
Category.findOne({ name: projectCategory }, function(err, categoryReturned){
    if (err) {
        console.log("Could not return a category");
        console.log(err);
        return;
    }
    if(categoryReturned == null){ //If category does not exist create a new category. 
        category = new Category({name : projectCategory});
        console.log(category);
        category.save(function(err, categoryCreated){
            console.log('New Category Created');
            callback(categoryCreated._id);
        });
    }else{
        callback(categoryReturned._id);
    }
});

}

Here is some sample input:

    "Name","Narrative","Address","Category","Lat","Lng","Year_Completed","Sponsors"
    "Raymond Avenue Neighborhood Garden Construction","","","3",34.0317141377768,-118.297777175903,"2010","??? (City of Los Angeles, The Garden Council)"
    "El Cariso Smart Garden Construction","","","3",34.3169450092985,-118.419291973114,"2010","L.A. County Parks and Recreation"
    "Central Basin Friendly Native Plants Garden","","","2",33.9955722580013,-118.144440650939,"2010","Central Basin Municipal Water District"

This creates two '3' Category entries. I know why. I dont know how to get around it.

var dataArray = new Array(YOUR DATA);
var index = 0;
(function(data) {
    var presentFunction = argument.callee;
    projectService.findCategory(data.Category, function(id){
        var Project = mongoose.model('Project');
        var project = new Project();
        project.name = data.Name;
        project.narrative = data.Narrative;
        project.address = data.address;
        project.category = id;
        project.lat = data.Lat;
        project.lng = data.Lng;
        project.save(function(err){
           console.log(project);
           if(err){
               console.log("There was an error in saving your project");
            console.log(err);
            return;
           }
           if (index > dataArray.length - 1) {
               return;
           } else {
               index++;
               presentFunction(dataArray[index]);
           }
        });
    });
})(dataArray[index]);

This ought to do it. The solution is crude with a lot of scope for improvement, but you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM