简体   繁体   English

遵循教程但无法弄清楚为什么我会收到此消息:“类型'() => WordArray'.ts(2339) 上不存在属性'子字符串'”

[英]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.我将假设您使用的是SHA256CryptoJS实现,因为它具有大小写。 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();
  }

More info can be found in the docs更多信息可以在文档中找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 通用:“T”类型上不存在属性“pass”.ts(2339) - Typescript generic : Property 'pass' does not exist on type 'T'.ts(2339) 为什么我在 querySelectorAll 上收到错误 TS2339 for checkbox property &#39;checked&#39; 不存在 - Why am I getting error TS2339 on querySelectorAll for checkbox property 'checked' does not exist 我该如何解决“'元素'类型上不存在属性'checkValidity'。” ts(2339) 错误 - How can i solve a "Property 'checkValidity' does not exist on type 'Element'." ts(2339) error Angular 管道上的 .includes 返回“属性 &#39;includes&#39; 在类型 &#39;never&#39;.ts(2339)&#39; 上不存在错误消息 - .includes on Angular pipe is returning 'Property 'includes' does not exist on type 'never'.ts(2339)' error message 我不知道为什么我收到以下错误“错误:元素类型无效:” - I can't figure out why I am getting the following error "Error: Element type is invalid:" 无法弄清楚为什么我得到“无法读取未定义的属性&#39;0&#39;”错误 - Can't figure out why I'm Getting “Cannot read property '0' of undefined” Error 将 class 转换为挂钩获取属性 'then' 在类型 '(dispatch: any) => Promise 上不存在<void> '.ts(2339)</void> - converting class to hooks getting Property 'then' does not exist on type '(dispatch: any) => Promise<void>'.ts(2339) 我不知道为什么我在这里得到“无法读取未定义的属性&#39;长度&#39;的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 输入字段上的“TS2339:属性...在类型...上不存在” - “TS2339: Property … does not exist on type …”, on input field TS2339:“字符串”类型上不存在属性“包含” - TS2339: Property 'includes' does not exist on type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM