Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to access a local variable value outside the scope of its function in nodejs

I want to compare the data of two files and for that, I’m reading that file using the fs module but since I want to compare the values so I thought to store the value in an external variable but when I do console.log(budget_details) I get nothing in console. Please someone help. Please point me out if my approach is wrong and if we don’t need to do that in nodejs. I’m new to nodejs.

import csv from 'csv-parser'
import fs from 'fs';

let budget_details
const budgetProcessing = (budget_file_path) => {
    try{
        fs.createReadStream(budget_file_path)
            .pipe(csv())
            .on('data', (row) => {
                budget_details = row
            })
            .on('end', () => {
                console.log('CSV file successfully processed');
            });
    }
    catch(error){
        console.log(error)
    }
}

budgetProcessing('budget.csv')

console.log(budget_details)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Let’s first explain why you don’t get the expected result, it doesnt have to do with scope actually:

import csv from 'csv-parser'
import fs from 'fs';

let budget_details
const budgetProcessing = (budget_file_path) => {
    try{
        fs.createReadStream(budget_file_path)
            .pipe(csv())
            .on('data', (row) => {
                budget_details = row
            })
            .on('end', () => {
                console.log('CSV file successfully processed');
            });
     }
    catch(error){
        console.log(error)
    }
}

budgetProcessing('budget.csv')

console.log(budget_details)

fs.createReadStream is not itslef exactly asynchronous but then we pipe the returned stream to csv-parser which does event based parsing, so even if you call budgetProcessing before the console.log(budget_details) the stream reading has most likely not runned yet and budget_details is still undefined.

To fix this, you could move this console.log(budget_details) where it is set like so:

    let budget_details
    const budgetProcessing = (budget_file_path) => {
        try{
            fs.createReadStream(budget_file_path)
                .pipe(csv())
                .on('data', (row) => {
                    budget_details = row
                    console.log(budget_details)
                })
                .on('end', () => {
                     console.log('CSV file successfully processed');
                });
         }
         catch(error){
            console.log(error)
        }
    }

    budgetProcessing('budget.csv')

but then the variable itself wouldnt serve any real purpose so instead you could do this:

    const budgetProcessing = (budget_file_path, callback) => {
        try{
            fs.createReadStream(budget_file_path)
                .pipe(csv())
                .on('data', (row) => {
                    callback(row)
                })
                .on('end', () => {
                     console.log('CSV file successfully processed');
                });
                
         }
         catch(error){
            console.log(error)
        }
    }

    budgetProcessing('budget.csv', (budget_details) => {
        console.log(budget_details) // or anything with budget_details
    })

Lastly, I want to make clear that the callback will be called for each row of the csv as specified in csv-parser‘s documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading