我试图在带有Redux / Saga的ReactJS应用中使用Web3.js
(以太坊JavaScript API)。
这是我的 GetWeb3 函数:
const GetWeb3 = () =>
new Promise((resolve, reject) => {
window.addEventListener("load", () => {
let web3 = window.web3;
const alreadyInjected = typeof web3 !== "undefined";
if (alreadyInjected) {
web3 = new Web3(web3.currentProvider);
console.log("Injected web3 detected.");
resolve(web3);
} else {
const provider = new Web3.providers.HttpProvider("http://127.0.0.1:7545");
web3 = new Web3(provider);
console.log("No web3 instance injected, using Local web3.");
resolve(web3);
}
});
});
export default GetWeb3;
而我正试图在这样的传奇中使用它:
import { call, put } from "redux-saga/effects";
import truffleContract from "truffle-contract";
import GetWeb3 from "../../../Utils/GetWeb3";
import SimpleStorageContract from "../../../Data/SimpleStorageContract.json";
import { loadWeb3Done, loadWeb3Error } from "./Web3.actionCreators";
export function* loadWeb3(action) {
try {
const web3 = yield call(GetWeb3());
const accounts = yield call(web3.eth.getAccounts());
console.log('accounts', accounts);
const contract = truffleContract(SimpleStorageContract);
contract.setProvider(web3.currentProvider);
const instance = yield call(contract.deployed());
yield put(
loadWeb3Done({
web3,
accounts,
contract: instance
})
);
} catch (e) {
yield put(loadWeb3Error(e));
}
}
当我的应用运行以上命令时,我看到此错误:
index.js:2178在检查调用时未捕获:参数[object Promise]不是 一个功能 stack_frame_overlay_proxy_console @ index.js:2178
GetWeb3.js:14检测到注入的web3。
并且我没有看到accounts
的控制台日志。
有什么想法我在这里可能做错了吗?