0

I'm using a CSV module as in the example below.
I was wondering if there is a way to make sure that all the fields aren't inserted as strings, e.g. the number 1.23 is not inserted as string "1.23".
It seems to make everything type string.

var csv = require('csv');
var q = async.queue(myCollection.insert.bind(myCollection), 50);

csv()
.from.path(req.files.myCSV.path, {columns: true})  
.transform(function(data, index, cb){
    q.push(data, function (err, res) {
        if (err) return cb(err);
        cb(null, res[0]);
    });
})
.on('end', function () {
     q.drain = function() {};  
})
2
  • Everything in CSV is a string, CSV has no types. The only benefit to not using quotes is in reducing the resulting file size. Sure, you might use the presence of quotes to infer the type, but that's super dangerous. Commented Mar 28, 2014 at 0:40
  • I'm talking about the actual module, is their some form of type inference option Commented Mar 28, 2014 at 11:06

1 Answer 1

1

In csv.transform(), before q.push(), you can convert fields using e.g. parseInt:

data.fieldX = parseInt(data.fieldX);
data.fieldY = parseFloat(data.fieldX);

You could also delete data.fieldY; or add fields data.fullName = data.first + ' ' + data.last;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.