在对象声明中使用 npm 哈希表时出现 TypeError 问题
Issue with TypeError using a npm hashtable in a object declaration
我正在尝试让一个简单的流工作以读取和解析 JSON 并将对添加到散列 table。最终我将把它导出为一个模块以在另一个程序中使用,但在调试时我遇到了这个错误:
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)
这是我的简单脚本:
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();
JSON 文件通常非常大,因此流,但在这个测试中它只是这样:
[{
"word": "Co",
"rate": 0.20654307524536533
},{
"word": "Issac",
"rate": 0.08174386920980926
},{
"word": "httpsaswarus",
"rate": 0.12835820895522387
},{
"word": "values",
"rate": 0.2151509086923788
}]
当我调试时,散列table 构造良好并且不是未定义的。读取函数之外的这种方法都可以正常工作。当我删除导致错误的行时,一切都打印得很好。我只是不明白如果 table 在这两种方法中工作正常,为什么会是未定义的。我觉得我错过了什么。如果有人对解决此问题有任何见解,将不胜感激!
以下是我用作参考的包的链接:
我相信您 运行 这个问题,因为 this
在您的回调函数中有不同的含义。
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']));
});
您可以通过执行以下简单操作来解决此问题:
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']));
});
或者,您可以通过以下操作在回调函数中更改 this
的含义:
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));
我正在尝试让一个简单的流工作以读取和解析 JSON 并将对添加到散列 table。最终我将把它导出为一个模块以在另一个程序中使用,但在调试时我遇到了这个错误:
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)
这是我的简单脚本:
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();
JSON 文件通常非常大,因此流,但在这个测试中它只是这样:
[{
"word": "Co",
"rate": 0.20654307524536533
},{
"word": "Issac",
"rate": 0.08174386920980926
},{
"word": "httpsaswarus",
"rate": 0.12835820895522387
},{
"word": "values",
"rate": 0.2151509086923788
}]
当我调试时,散列table 构造良好并且不是未定义的。读取函数之外的这种方法都可以正常工作。当我删除导致错误的行时,一切都打印得很好。我只是不明白如果 table 在这两种方法中工作正常,为什么会是未定义的。我觉得我错过了什么。如果有人对解决此问题有任何见解,将不胜感激!
以下是我用作参考的包的链接:
我相信您 运行 这个问题,因为 this
在您的回调函数中有不同的含义。
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']));
});
您可以通过执行以下简单操作来解决此问题:
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']));
});
或者,您可以通过以下操作在回调函数中更改 this
的含义:
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));