[英]Following a tutorial but can't figure out why I'm getting this message: "Property 'substring' does not exist on type '() => WordArray'.ts(2339)"
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)
我是一名初学者,正在学习关于可视代码的 yt 区块链教程,但在使用 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. this.hash
是返回某种列表的 function。 There is no substring
method on the function itself, only on the result. function 本身没有substring
方法,只有结果。
You need to use this.hash()
to call the function to get its result.您需要使用this.hash()
调用 function 以获取其结果。 Then this.hash().substring
might work.然后this.hash().substring
可能会起作用。
The issue is you are trying to use substring
method on a non String
class.问题是您尝试在非String
class 上使用substring
方法。 I'm going to assume you're using the CryptoJS
implementation of SHA256
given the capitalization.我将假设您使用的是SHA256
的CryptoJS
实现,因为它具有大小写。 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.如果不是,我将删除答案,但您需要将 hash 转换为String
class 才能使用substring
方法。 The SHA256
does not return a string and you need to convert it to one in order to apply string methods to it. SHA256
不返回字符串,您需要将其转换为字符串才能对其应用字符串方法。 Changing the calculateHash
method to this should make the code work:将calculateHash
方法更改为此应该使代码工作:
calculateHash(){
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this.nonce.toString()).toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.