无法读取属性错误(无法调用函数getNum)

时间:2019-08-15 18:32:20

标签: ethereum solidity web3 web3js

合同在html文件代码上方给出。 基本上是获取和设置数字。 我在代码中使用了promise。那是问题吗? ABI和合同地址都是正确的。 我使用了web3.currentProvider,因为我正在使用metamask。 我是区块链的初学者。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test for Web3</title>
    <!--<script src="https://cdn.jsdelivr.net/npm/web3@1.2.1/src/index.min.js"></script>-->
</head>
<body>
    <script>
        var web3;
        web3 = new Web3(web3.currentProvider);
        var contract = web3.eth.contract(
            [
    {
        "constant": false,
        "inputs": [
            {
                "name": "n",
                "type": "uint256"
            }
        ],
        "name": "setNum",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getNum",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
],"0x0c094f6ffbf8dbfde107e819e5060c509eab8951");
    contract.methods.getNum().call().then(function(result){
        console.log("Number: " + JSON.stringify(result));
    });
    </script>
</body>
</html>

在控制台上,错误:

未捕获的TypeError:无法读取未定义的属性'getNum'     在index.html:51

6:36:32 PM: Build ready to start
6:36:35 PM: build-image version: 9e0f207a27642d0115b1ca97cd5e8cebbe492f63
6:36:35 PM: build-image tag: v3.3.2
6:36:35 PM: buildbot version: ef8d0929ed0baabafd8bbb7d0b021e1fc24180c0
6:36:35 PM: Fetching cached dependencies
6:36:35 PM: Failed to fetch cache, continuing with build
6:36:35 PM: Starting to prepare the repo for build
6:36:36 PM: No cached dependencies found. Cloning fresh repo
6:36:36 PM: git clone https://github.com/Thr0TT1e/huddle_landing_page
6:36:37 PM: Preparing Git Reference refs/heads/master
6:36:37 PM: Found netlify.toml. Overriding site configuration
6:36:37 PM: Starting build script
6:36:37 PM: Installing dependencies
6:36:39 PM: Downloading and installing node v10.16.2...
6:36:40 PM: Downloading https://nodejs.org/dist/v10.16.2/node-v10.16.2-linux-x64.tar.xz...
6:36:40 PM: 
#####
6:36:40 PM:   7.8%
6:36:40 PM: 
###############################################################
6:36:40 PM: ######### 100.0%
6:36:40 PM: Computing checksum with sha256sum
6:36:40 PM: Checksums matched!
6:36:43 PM: Now using node v10.16.2 (npm v6.9.0)
6:36:44 PM: Attempting ruby version 2.6.2, read from environment
6:36:46 PM: Using ruby version 2.6.2
6:36:47 PM: Using PHP version 5.6
6:36:47 PM: Started restoring cached node modules
6:36:47 PM: Finished restoring cached node modules
6:36:47 PM: Installing NPM modules using NPM version 6.9.0
6:36:49 PM: npm
6:36:49 PM: WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
6:36:53 PM: npm WARN deprecated lodash.createcallback@2.4.4: This package is discontinued. Use lodash.iteratee@^4.0.0.
6:37:18 PM: > node-sass@4.12.0 install /opt/build/repo/node_modules/node-sass
6:37:18 PM: > node scripts/install.js
6:37:19 PM: Downloading binary from https://github.com/sass/node-sass/releases/download/v4.12.0/linux-x64-64_binding.node
6:37:21 PM: Download complete
6:37:21 PM: Binary saved to /opt/build/repo/node_modules/node-sass/vendor/linux-x64-64/binding.node
6:37:21 PM: Caching binary to /opt/buildhome/.npm/node-sass/4.12.0/linux-x64-64_binding.node
6:37:21 PM: > node-sass@4.12.0 postinstall /opt/build/repo/node_modules/node-sass
6:37:21 PM: > node scripts/build.js
6:37:22 PM: Binary found at /opt/build/repo/node_modules/node-sass/vendor/linux-x64-64/binding.node
6:37:22 PM: Testing binary
6:37:22 PM: Binary is fine
6:37:22 PM: > gifsicle@4.0.1 postinstall /opt/build/repo/node_modules/gifsicle
6:37:22 PM: > node lib/install.js
6:37:23 PM:   ✔ gifsicle pre-build test passed successfully
6:37:23 PM: > jpegtran-bin@4.0.0 postinstall /opt/build/repo/node_modules/jpegtran-bin
6:37:23 PM: > node lib/install.js
6:37:24 PM:   ✔ jpegtran pre-build test passed successfully
6:37:24 PM: > optipng-bin@5.1.0 postinstall /opt/build/repo/node_modules/optipng-bin
6:37:24 PM: > node lib/install.js
6:37:24 PM:   ✔ optipng pre-build test passed successfully
6:37:25 PM: npm
6:37:25 PM: notice created a lockfile as package-lock.json. You should commit this file.
6:37:25 PM: npm WARN
6:37:25 PM:  huddle-landing-page@0.0.1 No repository field.
6:37:25 PM: npm WARN optional
6:37:25 PM:  SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
6:37:25 PM: npm WARN
6:37:25 PM: notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
6:37:25 PM: added 925 packages from 552 contributors and audited 14495 packages in 37.17s
6:37:25 PM: found 0 vulnerabilities
6:37:26 PM: NPM modules installed
6:37:26 PM: Started restoring cached go cache
6:37:26 PM: Finished restoring cached go cache
6:37:26 PM: unset GOOS;
6:37:26 PM: unset GOARCH;
6:37:26 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.12.linux.amd64';
6:37:26 PM: export PATH="/opt/buildhome/.gimme/versions/go1.12.linux.amd64/bin:${PATH}";
6:37:26 PM: go version >&2;
6:37:26 PM: export GIMME_ENV='/opt/buildhome/.gimme/env/go1.12.linux.amd64.env';
6:37:26 PM: go version go1.12 linux/amd64
6:37:26 PM: Installing missing commands
6:37:26 PM: Verify run directory
6:37:26 PM: Executing user command: gulp build
6:37:28 PM: [15:37:28]
6:37:28 PM: Using gulpfile /opt/build/repo/gulpfile.js
6:37:28 PM: [15:37:28]
6:37:28 PM: Task never defined: build
6:37:28 PM: [15:37:28]
6:37:28 PM: To list available tasks, try running: gulp --tasks
6:37:28 PM: Skipping functions preparation step: no functions directory set
6:37:28 PM: Caching artifacts
6:37:28 PM: Started saving node modules
6:37:28 PM: Finished saving node modules
6:37:28 PM: Started saving pip cache
6:37:28 PM: Finished saving pip cache
6:37:28 PM: Started saving emacs cask dependencies
6:37:28 PM: Finished saving emacs cask dependencies
6:37:28 PM: Started saving maven dependencies
6:37:28 PM: Finished saving maven dependencies
6:37:28 PM: Started saving boot dependencies
6:37:28 PM: Finished saving boot dependencies
6:37:28 PM: Started saving go dependencies
6:37:28 PM: Finished saving go dependencies
6:37:38 PM: Error running command: Build script returned non-zero exit code: 1
6:37:38 PM: Failing build: Failed to build site
6:37:38 PM: failed during stage 'building site': Build script returned non-zero exit code: 1
6:37:38 PM: Finished processing build request in 1m3.374187994s

1 个答案:

答案 0 :(得分:1)

好像您正在使用1.X web3.js版本。在这种情况下,请使用var contract = new web3.eth.Contract。有关更多信息,请参见https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html

new web3.eth.Contract(jsonInterface[, address][, options])