0

I'm working on a personal project building a web application and teaching myself node.js. The part I'm working on involves uploading a CSV file, changing the CSV data into a JSON object and saving it in a field in mongodb

using mongoose I have this model defined. I'm using Robomongo to look at the contents of the DB and I can see email and passwordfields not problem

var userDataModel = new schema({
        email: {
                    type: String,
                    required: true,
                    unique: true,
                    validate: [validator.email, 'Not a valid email address!']
                },

            hashedPassword: {
                                type: String,
                                required: true,
                                validate: [checkLength, 'The Password is too short! Passwords must be at least '+REQ_PASS_LENGTH+' characters!']
                            },
            savedGraphs:{},
            uploadedFiles:{}
        })

The following is a file-handling module I wrote to call to be used during my route handling on the post request from the form containing the csv file.

var Converter               =   require("csvtojson").core.Converter
var currentdate             =   new Date()
exports.verifyFileTypeCSV   =   verifyFileTypeCSV
exports.saveFileDataCSV     =   saveFileDataCSV

function verifyFileTypeCSV(fileType){
    if(fileType!='application/csv'){
        return false
    }
    else{
        return true
    }
}

function saveFileDataCSV(file, username, password, model){
    model.findOne({ email :  username },
        function(err, user) {
            if (err){
                console.log('err')
            }
            if (!user){
                console.log('no user found!')
            }
            else{
                var csvConverter = new Converter()
                csvConverter.on("end_parsed",function(jsonObj){
                    jsonObj.date = currentdate.getDate()
                    user.uploadedFiles = jsonObj
                })
                csvConverter.from(file)
            }
    })
}

All the code seems to execute and I'm doing the exact same thing as I did the save the username/pass but once I run through it and check the DB, the userModel wont contain the parsed CSV data. I've printed the data to the console etc. so it's being parsed correctly and that's not my problem.

P.s

This is code I'm currently playing with, hence the if/else sections having console statements and nothing else.

1
  • 1
    Many things missing here. I don't see a user.save() stament. Do you have one?
    – gtsouk
    Mar 12, 2014 at 21:43

1 Answer 1

1

You´re missing to save the user-object back to MongoDB. Try to add:

   csvConverter.on("end_parsed",function(jsonObj){
         jsonObj.date = currentdate.getDate();
         user.uploadedFiles = jsonObj;
         user.save();
   });

I would also recommend you to add semicolons to the end of your statements, as it makes your code more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.