I've found several examples on S/O and otherwise, but they don't seem to be helping me.
I'm creating a private module in node that takes in a csv and converts it into a json object. It is properly outputting the correct value onto the command line, but the object itself is undefined.
exports.csvToJSON = function(file, callback) {
converter.fromFile(file, function(err, result) {
if (err) {
console.log('error: ', error)
} else {
return result;
callback(err, result);
}
});
console.log(callback)
}
I'm currently using the csvtojson module and have tried other similar packages. Articles I've referenced:
- Function Returns undefined object
- Why does this callback return undefined?
- Why is function return value undefined when returned in a loop? (although not entirely relevant as this is not a loop function)
- Callback returning undefined
I'm unsure if I'm just not understanding the callback correctly, but even if I console.log(result.type)
, it returns back undefined with or without the callback. I've also tried defining the callback like so:
exports.csvToJSON = function(file, callback) {
csvtojson().fromFile(file, function(err, result) {
if (err) {
console.log('error: ', error)
}
return result;
callback(result);
});
}
Here's an example of the console output:
Mirandas-MacBook-Pro:zendesktool mirandashort$ node ./controllers/update.js
[ { ticket:
{ ids: '3280805',
requester_email: 'miranda@barkbox.com',
status: 'pending',
comment: [Object],
subject: 'sup dog',
custom_fields: [Object],
add_tags: 'update_via_csv, dogs_are_cool, sup' } } ] undefined
Right now, since my other functions are dependent on this working, I'm only calling it in the file with exports.csvToJSON('update2.csv')
where update2.csv
is an actual file. Eventually this will be called inside another function in which I will be utilizing async, but I need this to work first. Additionally, that output seems to be linked to console.log(err)
when called by the second code block example, which I'm not to sure why.
Or if there's a way to do this altogether without csvtojson
, that's fine too. The only requirement be that a file in csv format can be returned as an object array.