我在getBlockHeight()
中定义了simpleChain.js
方法。我正在使用levelDb库通过另一个levelDb.js
进行数据持久化并导出所有模块。请原谅我的纯真,这是我第一次使用Javascript和Promises。
这是simpleChain.js
class Blockchain {
constructor(){
//Get block height from levelDB
this.getBlockHeight().then(height => {
//Check if block height is 0
console.log('Height of Blockchain: ' + height);
if (height === 0) {
//Create the Genesis Block - The first block in the blockchain
this.addBlock(
new Block('First block in the chain - Genesis block')
).then(() => console.log('Genesis Block created!'))
}
});
};
// Get block height
async getBlockHeight(){
//Return block height from levelDB
let blockHeight = await levelDB.getLevelBlockHeight();
return blockHeight;
};
这是levelDb.js
的代码,其中已导出方法
//Getting blockchain height from levelDB
getLevelBlockHeight : () => {
return new Promise(function(resolve, reject){
let height = 0;
db.createReadStream()
.on('data', function (data){
height++;
})
.on('error', function (error){
reject(error);
})
.on('close', function (){
resolve(height);
});
}).catch(() => {
console.log('Promise Error')
});
},
这是错误:
(节点:5896)UnhandledPromiseRejectionWarning:ReferenceError: 未定义getBlockHeight 在Blockchain.addBlock(repl:38:145) 在Blockchain.getBlockHeight.then.height(repl:29:81) 在 在process._tickDomainCallback(internal / process / next_tick.js:229:7)