Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ethereum web3.js中结构映射的访问映射_Ethereum_Solidity_Web3 - Fatal编程技术网

Ethereum web3.js中结构映射的访问映射

Ethereum web3.js中结构映射的访问映射,ethereum,solidity,web3,Ethereum,Solidity,Web3,假设我有这样的数据结构布局: struct ReviewStruct { string rating; ... } struct Restaurant { ... uint reviewCount; mapping(uint => ReviewStruct) reviews; } uint public restaurantCount = 0; mapping

假设我有这样的数据结构布局:

    struct ReviewStruct {
        string rating;
        ...
    }

    struct Restaurant {
        ...
        uint reviewCount;
        mapping(uint => ReviewStruct) reviews;
    }

    uint public restaurantCount = 0;
    mapping(uint => Restaurant) public restaurants;
然后,当我尝试访问JS应用程序中的内容时,它会起作用,但如果我尝试访问实际的评论,则不会起作用:

const restaurantCount = await review.methods.restaurantCount().call() // works
const restaurant = await review.methods.restaurants(2).call() // works
const reviewObj = await review.methods.restaurants(2).reviews(0).call() // throws an error

如何访问映射内部的映射(两者都与结构相关)?

在旧的ABI v1编码中,结构不能由公共函数返回,包括为映射自动生成的函数。这根本不可能。您需要创建自己的访问器函数,该函数以元组(值列表)的形式返回字段值,或者使用一个工具链


此外,我不确定是否会为映射的映射自动生成访问器函数,因此您可能会在任何情况下编写自己的函数。

您好,我已升级到solc 0.8.0,如您在回答中所述,该行仍然不起作用。请您提供一些关于如何获得
餐厅[2]的指导。例如,评论[3]
?(考虑上面的例子,它们已经在我的web3js代码中起作用了)