ReferenceError:初始化之前无法访问“ web3”

时间:2020-04-01 21:54:35

标签: node.js ethereum web3 metamask

chrome-browser-output chrome-console

在安装了create-react-app软件包之后,创建了web3.js文件并将console.log(web3.version)添加到App.js文件中,出现上述错误,并且不确定如何修复并使其正常工作。

我也尝试了以下方法,但仍然会引发相同的错误。

window.addEventListener('load', async () => {
// Modern dapp browsers...
if (window.ethereum) {
    window.web3 = new Web3(ethereum);
    try {
        // Request account access if needed
        await ethereum.enable();
        // Acccounts now exposed
        web3.eth.sendTransaction({/* ... */});
    } catch (error) {
        // User denied account access...
    }
}
// Legacy dapp browsers...
else if (window.web3) {
    window.web3 = new Web3(web3.currentProvider);
    // Acccounts always exposed
    web3.eth.sendTransaction({/* ... */});
}
// Non-dapp browsers...
else {
    console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
});

1 个答案:

答案 0 :(得分:0)

如果您尚未致电public class MergeSort { public static void main(String[] args) { int[] list = new int[] {3, 7, 5, 2, 9}; int[] result = mergeSort(list, 0, list.length); System.out.print("["); for (int i = 0; i < result.length; i++) { System.out.print(" " + result[i]); } System.out.println("]"); } public static int[] mergeSort(int[] list, int start, int end) { return F(list, start, end - 1); } public static int[] F(int[] list, int start, int end) { if (end - start < 2) { return list; } else { int mid = (start + end) / 2; F(list, start, mid); F(list, mid + 1, end); merge(list, start, mid, end); return list; } } public static void merge(int[] list, int start, int mid, int end) { int[] copy = new int[list.length]; for (int i = 0; i < list.length; i++) { copy[i] = list[i]; } int i = start; int k = start; int j = mid + 1; while (i <= mid && j <= end) { if (copy[i] <= copy[j]) { list[k] = copy[i]; i++; } else { list[k] = copy[j]; j++; } k++; } while (i <= mid) { list[k] = copy[i]; i++; k++; } while (j < end) { list[k] = copy[j]; j++; k++; } } } ,可能会发生错误。