UnhandledPromiseRejectionWarning:ReferenceError:未定义getBlockHeight

时间:2018-10-24 11:41:29

标签: javascript es6-promise blockchain leveldb

我在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)

0 个答案:

没有答案