简体   繁体   中英

Nodejs mongoose Mass/Batch update from file

So i have a csv file containing my information, i need to do a mass add/update

exports.add_questions_from_file = function (file_path, surveyid, callback)
{
    var U = [{}];
    fs.readFile(file_path, 'utf8', function(err, data){
    if (err){
        console.log(err);
        callback(err,null);
    }else{
        console.log(data);
        d = data.split(/\r\n|\n/);
        for (x=0;x <d.length;x++)
        {
            line = d[x].split(',');
            if (line[0] == "") {return};
            RQuestion.add_by_line (line,function (err, question)
            {
                    U.push({id:question.id});
                    console.log(U);
            });
        }   
    }

    }); 
    Survey.update({_id:surveyid},{$push:{"SurveyQuestions":U}},function (err,numAffected, rawResponse) {
                console.log(rawResponse);
                 RET = {"module":"survey","operation": "add", "status":"OK"};
                callback(RET);
    });
};

But even though im using callback functions the update seems to happen with the same object always, even the console.log here

U.push({id:question.id});
console.log(U);

returns the same object (even that all the other were created)

Im doing something wrong?

I see a few issues.

First for:

if (line[0] == "") {return};

Don't you mean to use a break or continue instead? Otherwise the entire function will quit if there is a blank line anywhere in the file. This is very important because Survey.update won't get called either.

Second: I assumed that RQuestion.add_by_line and Survey.update are doing something async like updating a database. Your code needs to be restructured to wait for those async items to complete before moving on to the next step. I'd recommend an npm package named async for that.

fs.readFile(file_path, 'utf8', function(err, data){
 if (err){
     console.log(err);
     callback(err,null);
 }else{
    d = data.split(/\r\n|\n/);

    async.map(d, function(line, callback) {
        //this function is called for each line
       add_by_line (line,function (err, question)
       {
         callback(err,{id:question.id});
       });
    }, function(err, results) {
        //this function is called when all of the items are done
        console.log("done with async");
        console.dir(results);
        Survey.update({_id:surveyid},{$push:{"SurveyQuestions":results},function (err,numAffected, rawResponse) {
                console.log(rawResponse);
                RET = {"module":"survey","operation": "add", "status":"OK"};
                callback(RET);
        });

    });

 }
});

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