Node.js Nodejs工作不正常,给我类型错误
我有这个功能:Node.js Nodejs工作不正常,给我类型错误,node.js,Node.js,我有这个功能: const getWeb3Instance = network => { let web3 = new Web3(new Web3.providers.HttpProvider(wrapConstants.constants.RPC)) if (network === 'ropsten') { web3 = new Web3(new Web3.providers.HttpProvider(wrapConstants.constants.ROPSTEN_RPC));
const getWeb3Instance = network => {
let web3 = new Web3(new Web3.providers.HttpProvider(wrapConstants.constants.RPC))
if (network === 'ropsten') {
web3 = new Web3(new Web3.providers.HttpProvider(wrapConstants.constants.ROPSTEN_RPC));
} else if (network === 'rinkeby') {
web3 = new Web3(new Web3.providers.HttpProvider(wrapConstants.constants.RINKEBY_RPC));
} else if (network === 'fnbd') {
web3 = new Web3(new Web3.providers.HttpProvider(wrapConstants.constants.PRIVATE_RPC));
}
return web3;
}
这就是我使用这些函数的方式
router.post('/chooseNetwork',(req,res)=> {
const network = req.body.network
network.length === 0 ? res.send('You must choose network') : res.send(controllers.getWeb3Instance((network)))
})
将请求发送到此端点后,我收到以下错误:
TypeError:将循环结构转换为JSON
我真的不明白为什么会出现这个错误,有什么建议吗?这只是意味着您试图序列化的结构具有自引用元素<代码>设x={};x、 x=x将无法序列化。值得注意的是,通过使用一个简单的查找表来解析常量,然后调用
new Web3(new Web3.providers.HttpProvider(lookup[network]|c.RPC))
是否遵循代码问题?`const wrapConstants={constants,contracts}module.exports=wrapConstants`这是您的结构。在它的某个地方,有些东西在循环。既然您正在转储一个原始的Web3
东西,不管是什么,您要么a)不想这样做,要么b)确实想这样做,但只是它的一个属性。@tadman因此基本上可以缩短代码,但保留相同的功能