javascript - How to return my CSV data from my service - asyncawait issue? - Stack Overflow

I am attempting to load some CSV data in my API such that I can manipulate it and pass through to my fr

I am attempting to load some CSV data in my API such that I can manipulate it and pass through to my front end, however I am having a few issues returning the data.

I am using fast-csv to do the parsing here.

service.js

const fs = require('fs');
const csv = require('fast-csv');

module.exports.getFileContents = (filepath) => {
    let data = [];

    fs.createReadStream(filepath)
        .pipe(csv.parse({ headers: true }))
        .on('error', error => console.error(error))
        .on('data', row => data.push(row))
        .on('end', () => {
            console.log(data) // This will print the full CSV file fine
            return data;
        });
};

routes.js

router.get('/data/:filename', (req, res) => {
  const file = FS.getFileContents(testUrl + '/' + req.params.filename + '.csv');
  console.log(file); // This prints 'undefined'
  res.send(file);
});

I can print out the CSV contents fine from the service, but I just get 'undefined' from the actual routes. Can somebody please point out what I'm missing?

I am attempting to load some CSV data in my API such that I can manipulate it and pass through to my front end, however I am having a few issues returning the data.

I am using fast-csv to do the parsing here.

service.js

const fs = require('fs');
const csv = require('fast-csv');

module.exports.getFileContents = (filepath) => {
    let data = [];

    fs.createReadStream(filepath)
        .pipe(csv.parse({ headers: true }))
        .on('error', error => console.error(error))
        .on('data', row => data.push(row))
        .on('end', () => {
            console.log(data) // This will print the full CSV file fine
            return data;
        });
};

routes.js

router.get('/data/:filename', (req, res) => {
  const file = FS.getFileContents(testUrl + '/' + req.params.filename + '.csv');
  console.log(file); // This prints 'undefined'
  res.send(file);
});

I can print out the CSV contents fine from the service, but I just get 'undefined' from the actual routes. Can somebody please point out what I'm missing?

Share Improve this question asked Apr 7, 2020 at 20:09 physicsboyphysicsboy 6,39822 gold badges77 silver badges140 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

This is a mon problem with JavaScript code, in the following.

.on('end', () => {
    console.log(data);
    return data;
 });

Your on-end handler is an anonymous callback function (because of () =>), so when you return data, you are returning data out of your on-end handler callback function. You are not returning data out of your enclosing getFileContents() function.

Here's a typical way to write this kind of code:

const getFileContents = async (filepath) => {
  const data = [];

  return new Promise(function(resolve, reject) {
    fs.createReadStream(filepath)
      .pipe(csv.parse({ headers: true }))
      .on('error', error => reject(error))
      .on('data', row => data.push(row))
      .on('end', () => {
        console.log(data);
        resolve(data);
      });
  });
}

And then, call it as follows, though this must be within an async function:

const data = await getFileContents('games.csv');

What's happened here is as follows:

  • your getFileContents is now async and returns a promise
  • the CSV data will be available when resolve(data) is executed
  • the caller can await the fulfillment/resolution of this promise to get the data

You could just create a Promise in the service and return it. Once the job is done, resolve it. The returned Promise will wait until it is resolved.

service.js

  const fs = require('fs');
  const csv = require('fast-csv');

   module.exports.getFileContents = (filepath) => {
    let data = [];
    return new Promise((resolve) => {

      fs.createReadStream(filepath)
        .pipe(csv.parse({ headers: true }))
        .on('error', error => console.error(error))
        .on('data', row => data.push(row))
        .on('end', () => {
            resolve(data);
        });
    }
  };

routes.js

router.get('/data/:filename', (req, res) => {
  const file = await FS.getFileContents(testUrl + '/' + req.params.filename + '.csv');
  console.log(file); // This prints only after it is resolved
  res.send(file);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745594249a4635013.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信