I am trying to access @cols from anom function, it is coming as undefined (at second last line) even though it is defined.
csv = require 'csv' class Inventory constructor: (@file) -> @cols = {} #Read the file and push to cols csv(). fromPath(@file,columns: true ). on 'data', (d,index)-> #push to cols console.log @cols inventory = new Inventory(__dirname + '/sample.csv')
You need to use the fat arrow instead of the slim one.
(d, index)=>
use => instead of -> so you can use @ and get a reference to the outer instance
csv = require 'csv'
class Inventory
constructor: (@file) ->
@cols = {}
#Read the file and push to cols
csv().
fromPath(@file,columns: true).
on 'data', (d,index) =>
#push to cols
console.log @cols
inventory = new Inventory(__dirname + '/sample.csv')