Martin Carre
Martin Carre

Reputation: 1249

Node.JS: Promise yields to null when writing to the file

I've been working on a problem for a while now and I can't seem to understand fully the origin. My app has a workflow that goes like this:

  1. Get a array of items for individual request
  2. Make the individual requests for each item of this array: Promise.all(array.map(individualRequest))
  3. Take the info from each request and change it to a specific layout
  4. Record the modified info of all the items in the array in a JSON file

Everything seems to be working seamlessly up to the 4th step. In the 3rd step I get the objects modified correctly but when it comes back to the main (each step has a dedicated module), the 4th step writes: [null, null, null...] (null for each item in the orginal array) in the JSON file.

I'm not sure how to put it in code and make it simple (without posting the entire original code) but you can find a full version here: https://github.com/Ardzii/mongoNode

I know that the item info is being transformed correctly because I left a console log in the transformation module for verification and it gives me the correct information:

[ADDED]: {"name":"DASHEUR","a":["151.400000","1","1.000"],"b":["148.500000","1","1.000"],"c":["151.577960","0.01000000"],"v":["1861.79012466","3063.32653118"],"p":["151.527639","150.811326"],"t":[688,1205],"l":["146.599930","146.599930"],"h":["156.118780","156.118780"],"o":"148.000000"}

I have no idea why, when it comes back to the main (app.js):

async function worker() {
      var tickerList = await getTickerList();
      var tickerInfo = await getTickerInfo(tickerList);
      var filename = `./modules/batches/${new Date().toISOString()}batch.json`;

      fs.writeFile(filename, JSON.stringify(tickerInfo), (err) => {
          if (err) throw err;
          console.log("[SUCCESS]: All tickers processed successfully!");
      });
  }

  worker();

I get an awful

[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]

stored in the JSON file... (again, that's one 'null' for each item in the array)

Thanks in advance for your help and I hope I was able to keep it clear and simple!

Upvotes: 2

Views: 73

Answers (1)

Teh
Teh

Reputation: 3366

modules/transformTicker.js, line 22

function transformTicker(ticker) {
  var result = {};
  Object.keys(ticker).forEach((k) => {
    // Object.keys(ticker[pair]).forEach((k) => {
      // console.log(k);
      result= {
        name: k,
        a: ticker[k].a,
        b: ticker[k].b,
        c: ticker[k].c,
        v: ticker[k].v,
        p: ticker[k].p,
        t: ticker[k].t,
        l: ticker[k].l,
        h: ticker[k].h,
        o: ticker[k].o,
      }
      console.log(`[ADDED]: ${JSON.stringify(result)}`);
      return result;
    // });
  });
}

Your 'return' statement is inside the forEach function scope, while it should be outside, after the for-each loop.

Corrected version:

function transformTicker(ticker) {
  var result = {};
  Object.keys(ticker).forEach((k) => {
    // Object.keys(ticker[pair]).forEach((k) => {
      // console.log(k);
      result= {
        name: k,
        a: ticker[k].a,
        b: ticker[k].b,
        c: ticker[k].c,
        v: ticker[k].v,
        p: ticker[k].p,
        t: ticker[k].t,
        l: ticker[k].l,
        h: ticker[k].h,
        o: ticker[k].o,
      }
      console.log(`[ADDED]: ${JSON.stringify(result)}`);
    // });
  });
  return result;
}

Upvotes: 3

Related Questions