× 错误:Alchemy URL 协议必须是 http、https、ws 或 wss 之一。收到:未定义的React.js

Tes*_*pia 1 javascript alchemy reactjs smartcontracts

我只是在 VS code 中克隆智能合约 React 应用程序并安装节点模块。当我尝试 npm start 时,它给了我错误“错误:Alchemy URL 协议必须是 http、https、ws 或 wss 之一。收到:未定义的 React.js”。

我怎么解决这个问题?谢谢

tha*_*ech 6

根据炼金术文档

将 websockets 与web3-alchemysdk 一起使用需要使用 websocket URL 设置 web3 对象,如下所示:

const { createAlchemyWeb3 } = require("@alch/alchemy-web3");

const web3 = createAlchemyWeb3("wss://eth-mainnet.ws.alchemyapi.io/ws/<api-key>");
Run Code Online (Sandbox Code Playgroud)

设置正确后,您应该能够拨打电话,例如:

web3.eth.getBlockNumber().then(console.log);  // -> 7946893
Run Code Online (Sandbox Code Playgroud)

您看到的错误是由于undefined React.js传递到 alchemy web3 实例化的参数不正确所致,基本上是:

const web3 = createAlchemyWeb3( undefined React.js ); // the error is here
Run Code Online (Sandbox Code Playgroud)

您需要弄清楚为什么会undefined React.js出现错误,一旦修复了该错误,您的应用程序就应该能够加载!