2017-12-30 30 views
0

web3 (1.0.0-beta.27)를 사용하여 계약서를 배포하고 이벤트를 수신 대기하는 스크립트를 작성하고 있습니다.Web3 : 제공 업체 없음

const Web3 = require('web3'); 

let web3 = new Web3('http://localhost:8545'); 

let MyContract = new web3.eth.Contract(abi); 

let accounts = await web3.eth.getAccounts(); 
let contract = await MyContract.deploy({data: code}) 
    .send({from: accounts[0], gas: 1000000}); 

contract.events.MyEvent((error, result) => { 
    if (error) { 
    return console.error(error); 
    } 
} 

내가 코드를 실행할 때, 나는 다음과 같은 오류가 발생합니다, 그러나 :

여기에 코드의 단순화 된 버전입니다

Error: No provider set. 
    at Subscription.subscribe (/node_modules/web3-core-subscriptions/src/subscription.js:199:20) 
    at Contract._on (/node_modules/web3-eth-contract/src/index.js:634:18) 

나의 이해했다 그 Web3에 URL을 전달 생성자가 공급자를 설정하지만 작동하지 않는 것 같습니다.

답변

0

Web3을 생성 할 때 HttpProvider을 전달해야합니다. 변경

let web3 = new Web3('http://localhost:8545');

let web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

또는, 당신은 Web3을 만든 후 공급자 설정할 수 있습니다

const web3 = new Web3(); 

web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); 
+0

여전히 작동하지 않습니다. [web3 documentation] (http://web3js.readthedocs.io/en/1.0/web3.html#setprovider)는 두 형식이 동등한 것임을 암시하는 것으로 보입니다. –