It seems that on my computer/web browser Google Chrome is slow to inject meta-mask. Therefore I have to write something like this for my App to actually get the web3 instance instead of failing:
setTimeout(function(){
if (typeof web3 !== 'undefined'){
console.log('getting web3');
App.web3Provider = web3.currentProvider;
}
else {
console.log('No Web3 instance given!');
// flash modal saying "please download Metamask"
}
web3 = new Web3(App.web3Provider);
return App.initContract(web3);
}, 500);
However, this doesn't play nice with Truffle-Contract js library, which needs the web3 instance straight away. Does anyone have a workaround or a fix for this? Thanks.