Web3js

TypeError: (中間值).at 不是函式

  • September 21, 2017

這是我的程式碼:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Web3 from 'web3';

var ETHEREUM_CLIENT = new Web3(new Web3.providers.HttpProvider("http://localhost:8585"));

var peopleContractABI = [{"constant":true,"inputs":[],"name":"getContractAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getPeople","outputs":[{"name":"","type":"bytes32[]"},{"name":"","type":"bytes32[]"},{"name":"","type":"uint256[]"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_age","type":"uint256"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"people","outputs":[{"name":"firstName","type":"bytes32"},{"name":"lastName","type":"bytes32"},{"name":"age","type":"uint256"}],"payable":false,"type":"function"}]
var peopleContractAddress = '0xd2304e34972744ac6eba5548b54e7377539e1085'

var peopleContract = new ETHEREUM_CLIENT.eth.Contract(peopleContractABI).at(peopleContractAddress);

這是我得到的錯誤:

TypeError: (中間值).at 不是函式

▶ 1 stack frames were collapsed.
Object../src/App.js
src/App.js:11
  8 | var peopleContractABI = [{"constant":true,"inputs":[],"name":"getContractAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getPeople","outputs":[{"name":"","type":"bytes32[]"},{"name":"","type":"bytes32[]"},{"name":"","type":"uint256[]"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_firstName","type":"bytes32"},{"name":"_lastName","type":"bytes32"},{"name":"_age","type":"uint256"}],"name":"addPerson","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"people","outputs":[{"name":"firstName","type":"bytes32"},{"name":"lastName","type":"bytes32"},{"name":"age","type":"uint256"}],"payable":false,"type":"function"}]
  9 | var peopleContractAddress = '0xd2304e34972744ac6eba5548b54e7377539e1085'
 10 | 
> 11 | var peopleContract = new ETHEREUM_CLIENT.eth.Contract(peopleContractABI).at(peopleContractAddress);
 12 | 
 13 | class App extends Component {
 14 |   componentWillMount(){

最初,我收到錯誤:

“× TypeError: ETHEREUM_CLIENT.eth.contract 不是函式”

然後我按照第二個解決方案將’ETHEREUM_CLIENT.eth.contract(abi)‘變成’new ETHEREUM_CLIENT.eth.Contract(abi)‘來遵循這個解決方案

當我保存這些更改時,我的錯誤更改為“TypeError: (intermediate value).at is not a function”。

有任何想法嗎?

改成peopleContract這樣:

var peopleContract = new ETHEREUM_CLIENT.eth.Contract(peopleContractABI, peopleContractAddress);

關於為您的合約對象指定參數的 web3 文件:https ://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#parameters

編輯:上述解決方案適用於web3.js 1.0.at適用於 web3.js 0.xx

引用自:https://ethereum.stackexchange.com/questions/23932