Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get a single record? #463

Open
gaurav-mtalkz opened this issue Dec 30, 2022 · 0 comments
Open

How to get a single record? #463

gaurav-mtalkz opened this issue Dec 30, 2022 · 0 comments

Comments

@gaurav-mtalkz
Copy link

I need to get only the first record of a large CSV in JSON format.

The idea is to upload the file, parse the first record, get columns verified by the users (with sample data of first record), and then post approval finally process the records for insertion to a mongodb collection.

I'm using the below code to parse the first record:

    const sampleRecord = await new Promise((resolve, reject) => {
      csv({
        trim: true,
        checkType: true,
        ignoreEmpty: true,
        maxRowLength: 65535
      })
      .fromFile(filePath)
      .subscribe(resolve, reject);
    });

    console.log('res', sampleRecord);
    return { filePath, sampleRecord };

Now console.log is able to print the record, whereas return gives me an empty object.

What am I missing here?

I've also tried below to stop processing after the first record, to no avail:

      const parser = csv({
        trim: true,
        checkType: true,
        ignoreEmpty: true,
        maxRowLength: 65535
      })
      .fromFile(filePath)
      .subscribe(obj => {
        parser.emit('end');
        resolve(obj);
      }, reject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant