I'm a beginner following a yt blockchain tutorial on visual code yet i'm getting this message when using substring: Property 'substring' does not exist on type '() => WordArray'.ts(2339)
class Block {
constructor(index, timestamp, data, previousHash = ''){
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash;
this.nonce = 0;
}
calculateHash(){
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this.nonce.toString());
}
mineBlock(difficulty){
while(this.hash().substring(0, difficulty) !== Array(difficulty + 1).join("0")){
this.nonce++;
this.hash = this.calculateHash();
}
console.log("Block mined: " + this.hash);
}
}
this.hash
is a function that returns some kind of list. There is no substring
method on the function itself, only on the result.
You need to use this.hash()
to call the function to get its result. Then this.hash().substring
might work.
The issue is you are trying to use substring
method on a non String
class. I'm going to assume you're using the CryptoJS
implementation of SHA256
given the capitalization. If not I'll delete the answer, but you need to convert the hash to a String
class in order to use the substring
method. The SHA256
does not return a string and you need to convert it to one in order to apply string methods to it. Changing the calculateHash
method to this should make the code work:
calculateHash(){
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this.nonce.toString()).toString();
}
More info can be found in the docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.