I'm trying to get a simple stream working to read and parse JSON and add pairs to a hashtable. Eventually I am going to export this as a module to use in another program, but in debugging I'm stuck at this error:
this.hashtable.put(data['word'], data['rate']);
^
TypeError: Cannot read property 'put' of undefined
at Stream.<anonymous> (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/test/test-stream.js:18:22)
at Stream.emit (events.js:315:20)
at drain (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/through/index.js:36:16)
at Stream.stream.queue.stream.push (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/through/index.js:45:5)
at Parser.parser.onValue (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/JSONStream/index.js:118:16)
at Parser.proto.emit (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/jsonparse/jsonparse.js:337:8)
at Parser.proto.pop (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/jsonparse/jsonparse.js:332:8)
at Parser.proto.onToken (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/jsonparse/jsonparse.js:402:12)
at Parser.parser.onToken (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/JSONStream/index.js:128:12)
at Parser.proto.write (/mnt/c/users/joeys/Desktop/RumbleUp/message-data/dev/node_modules/jsonparse/jsonparse.js:135:34)
Here is my simple script:
var JSONStream = require('JSONStream')
var fs = require('fs');
var Hash = require('simple-hashtable');
function obj() {
this.hashtable = new Hash();
console.log(this.hashtable.isEmpty());
this.hashtable.put('yo', 'response');
this.stream = fs.createReadStream('./output-1.json', { encoding: 'utf8' }).pipe(JSONStream.parse('*'));
this.read = function(){
this.stream.on('data', function(data){
console.log(data['word']);
this.hashtable.put(data['word'], data['rate']);
//console.log(this.hashtable.get(data['word']));
});
}
}
var temp = new obj();
temp.read();
The JSON file is usually very large, hence the stream, but in this testing it is simply this:
[{
"word": "Co",
"rate": 0.20654307524536533
},{
"word": "Issac",
"rate": 0.08174386920980926
},{
"word": "httpsaswarus",
"rate": 0.12835820895522387
},{
"word": "values",
"rate": 0.2151509086923788
}]
When I debug, the hashtable is constructed fine and isn't undefined. This methods outside of the read function both work fine. When I remove the line causing the error, everything prints perfectly fine. I just don't understand why the table would be undefined if it works fine in those two methods. I feel like I'm missing something. If anyone has any insight on solving this issue, it would be much appreciated!
Here are links to the packages I used for reference:
I believe you are running into this issue because this
has a different meaning within your callback function.
this.stream.on('data', function(data){
console.log(data['word']);
this.hashtable.put(data['word'], data['rate']); // this isn't this
//console.log(this.hashtable.get(data['word']));
});
You could resolve this by doing something as simple as:
const that = this;
this.stream.on('data', function(data){
console.log(data['word']);
that.hashtable.put(data['word'], data['rate']); // that, not this
//console.log(this.hashtable.get(data['word']));
});
Or, you could change the meaning of this
within the callback function by doing:
this.stream.on('data', function(data){
console.log(data['word']);
this.hashtable.put(data['word'], data['rate']); // this is this
//console.log(this.hashtable.get(data['word']));
}.bind(this));