-1

I'm currently working on this function, and the return value returns with undefined. I'm not sure if it is my lack of understanding how javascript operates, or it requires some tricky voodoo to make my code work.

var fs = require('fs');
var _ = require('underscore');


function eventValue(current, choice) {
 var output = [];

 if (current != null) {


  var json;
  fs.readFile('./json.json', 'utf8', function(err, data) {
   if (err) {
    throw err
   }

   json = JSON.parse(data);

   // filter by ID 
   var filtered = _.filter(json, {
    'ID': current
   });
   //console.log(filtered);

   // Get ID of Object
   var ID = _.pluck(filtered, 'ID');
   // Get Next value of object
   var NEXT = _.pluck(filtered, 'next');


   // get nested 'choice's 'next' value
   var collect = _.pluck(_.filter(
    _.flatten(
     _.pluck(filtered, 'choice')), {
     'choice': choice
    }), 'next');


   var stringID = String(ID);
   var stringNext = String(NEXT + collect);

   output = [stringID, stringNext];

   return output;

  })
 } else console.log("[[error]] please populate eventValue()");
};


var a = eventValue("001001A01B01");
console.log(a);
  • Does this answer your question? [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) – viam0Zah Jul 11 '20 at 21:41

1 Answers1

0

You are trying to use value returned from asynchronous function callback which you can not.

Refer: How to get returned value by function with callback inside

Community
  • 1
  • 1
Majeed Siddiqui
  • 548
  • 2
  • 9