0

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:

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.

Miranda Short
  • 94
  • 1
  • 11
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Daniel A. White Apr 15 '18 at 22:41
  • Which `console.log` is returning undefined? Can you include an example calling your function? – Catalyst Apr 15 '18 at 22:41
  • @DanielA.White while I do use async later in this app, that post itself doesn't quite help me understand my problem. From what I can tell, my formatting doesn't look far off from what the responses suggest. Additionally, I'm not using Ajax. – Miranda Short Apr 15 '18 at 22:54
  • @Catalyst both `console.log`s return the proper object on the command line, but if I do `type` on any results, it's always undefined. Additionally, JSON.parse cannot evaluate the result, although JSON.stringify can (but will still return undefined). I'll update my post with an example of the call as well as console output. – Miranda Short Apr 15 '18 at 22:56
  • @DanielA.White I have attempted to add in `setImmediate` for the callback and still no luck. – Miranda Short Apr 16 '18 at 19:19

1 Answers1

0

Got it. I just used waterfall to put the two individual modeles together:

exports.csvToJSON = function(file, callback) {
  csvtojson().fromFile(file, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      callback(null, result[0]);
    }
  });
}

exports.zdUpdateMany = function(data, callback) {
  credentials.config.tickets.updateMany(3280805, data, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      callback(false, result);
    }
  });
}


// function to update tickets
exports.processUpdate = function(file, callback) {
  async.waterfall([
    async.apply(exports.csvToJSON, file),
    exports.zdUpdateMany
  ], function(err, result) {
    if (err) {
      console.log(err);
    } else {
      console.log(result);
    }
    callback(err, result);
  });
}
Miranda Short
  • 94
  • 1
  • 11