2017-01-01 7 views
1

첫 번째 dapp이 작동하도록하려고합니다. 나는 가깝다는 것을 알고 있지만 web3에 계속 문제가있다.잡히지 않은 ReferenceError : web3이 window.onload에서 정의되지 않았습니다.

나는 PowerShell을를 통해 testrpc 노드를 실행, 윈도우 10작업입니다. 트뤼플을 사용하여 & 샘플 파일을 내 폴더로 설정 한 다음 컴파일하고 마이그레이션하십시오. 나는 에서 HTML 파일을 열 수 있어요

var accounts; 
var account; 

function setStatus(message) { 
    var status = document.getElementById("status"); 
    status.innerHTML = message; 
}; 

function refreshBalance() { 
    var meta = MetaCoin.deployed(); 

    meta.getBalance.call(account, {from: account}).then(function(value) { 
    var balance_element = document.getElementById("balance"); 
    balance_element.innerHTML = value.valueOf(); 
    }).catch(function(e) { 
    console.log(e); 
    setStatus("Error getting balance; see log."); 
    }); 
}; 

function calcPremium() { 
    var premium = parseInt(document.getElementById("benefit").value)/10000; 
    document.getElementById("monthlyPremium").innerHTML = "    Monthly Premium: $"+premium.toFixed(2); 

}; 

function sendCoin() { 
    var meta = MetaCoin.deployed(); 

    var amount = parseInt(document.getElementById("monthlyPremium").value); 
    var receiver = document.getElementById("receiver").value; 

    setStatus("Initiating transaction... (please wait)"); 

    meta.sendCoin(receiver, amount, {from: account}).then(function() { 
    setStatus("Transaction complete!"); 
    refreshBalance(); 
    }).catch(function(e) { 
    console.log(e); 
    setStatus("Error sending coin; see log."); 
    }); 
}; 

window.onload = function() { 
    web3.eth.getAccounts(function(err, accs) { 
    if (err != null) { 
     alert("There was an error fetching your accounts."); 
     return; 
    } 

    if (accs.length == 0) { 
     alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly."); 
     return; 
    } 

    accounts = accs; 
    account = accounts[0]; 

    refreshBalance(); 
    }); 
} 

:

나는 내가 에서 아무것도 변경 생각하지 않는다는 송로 버섯에 의해 만들어 파일을 app.js ... 여기에 코드입니다 Chrome 브라우저, MetaMask 플러그인 사용. 그러나 web3 오류 문제로 인해 계약과 상호 작용할 수없는 것으로 보입니다. 정확한 메시지는이 게시물의 제목입니다.

미리 도움을 주셔서 감사합니다.

답변

2

시도해보십시오. 나는 onload이 문제를주고 있다고 생각합니다.

$(window).load function() { 
    web3.eth.getAccounts(function(err,accs) { 
    if (err != null) { 
     alert("There was an error fetching your accounts."); 
     return; 
    } 

    if (accs.length == 0) { 
     alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly."); 
     return; 
    } 

    accounts = accs; 
    account = accounts[0]; 

    refreshBalance(); 
    }); 
} 
+1

감사합니다. @Ranadip Dutta! 나는 당신이 보여준대로 변화를 만들었고 $가 정의되지 않았다는 오류 메시지가 나타납니다. 그래서 $를 제거했고 페이지에 오류가 없습니다. 문제의 코드 행은 이제 다음과 같습니다. '(창) .load 함수() { ... ' –

+0

실제로이 문제는 .load 함수에서 발생합니다. 여러 가지 방법으로 전달할 수 있습니다. 어쨌든, 그것이 잘 작동한다면 충분합니다. :) –