返回固体中的阵列或对象列表

发布于 2025-02-05 18:03:58 字数 910 浏览 0 评论 0原文

我是牢固性的新手,并尝试在我在Internet上发现的代码中进行一些修改。下面给出的是Vanet合同:

contract VANET {
    string name;
    string owner;
    string number;
    string license;
    event Vehicle(
        string name,
        string owner,
        string number,
        string license
    );
    function setVehicle(string _name, string _owner, string _number, string _license) public{
        name=_name;
        owner=_owner;
        number=_number;
        license=_license;
        Vehicle(_name, _owner, _number, _license);
    }
    function getVehicle() public constant returns (string, string, string, string){
        return (name, owner, number, license);
    }

当我尝试使用以下代码致电GetVehicle()时:

 VANET.methods.getVehicle().call().then(r=>{
        console.log(r);  //outputs only the last vehicle added
      })

仅显示最后添加的车辆。我想要的是使用setVehicle()函数添加到区块链中的所有车辆的列表。我该怎么做?

I'm new to Solidity and trying to make some modification in the code I found on Internet. Given below is the VANET contract:

contract VANET {
    string name;
    string owner;
    string number;
    string license;
    event Vehicle(
        string name,
        string owner,
        string number,
        string license
    );
    function setVehicle(string _name, string _owner, string _number, string _license) public{
        name=_name;
        owner=_owner;
        number=_number;
        license=_license;
        Vehicle(_name, _owner, _number, _license);
    }
    function getVehicle() public constant returns (string, string, string, string){
        return (name, owner, number, license);
    }

When I try to call getVehicle() using following code:

 VANET.methods.getVehicle().call().then(r=>{
        console.log(r);  //outputs only the last vehicle added
      })

it shows only last added vehicle. What I want is the list of all the vehicles that are added to the blockchain using setVehicle() function. How can I do That?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九歌凝 2025-02-12 18:03:58

每当您调用setVehicle()时,重写存储属性的值值。

因此,将所有这些值分组为struct(类型类型),然后将这些结构放在数组中,以便您可以保留所有输入值。然后,您可以简单地检索整个数组:

pragma solidity ^0.8;

contract VANET {
    struct Vehicle {
        string name;
        string owner;
        string number;
        string license;
    }
    Vehicle[] private vehicles;

    event VehicleAdded(
        string name,
        string owner,
        string number,
        string license
    );

    function addVehicle(string memory _name, string memory _owner, string memory _number, string memory _license) public {
        vehicles.push(
            Vehicle(_name, _owner, _number, _license)
        );
        emit VehicleAdded(_name, _owner, _number, _license);
    }

    function getVehicles() public view returns (Vehicle[] memory) {
        return vehicles;
    }
}

看来您的摘要正在使用固定的固定版本(我猜是0.4),然后我使用最新版本的0.8编写了示例。这些版本之间存在一些微小的语法变化,因此请记住这一点。

Each time you invoke setVehicle(), it rewrites values of the storage properties.

So the most straightforward way to group all those values in a struct (an object-like type) and then place those structs in an array, so that you can keep all input values. Then you can simply retrieve the whole array:

pragma solidity ^0.8;

contract VANET {
    struct Vehicle {
        string name;
        string owner;
        string number;
        string license;
    }
    Vehicle[] private vehicles;

    event VehicleAdded(
        string name,
        string owner,
        string number,
        string license
    );

    function addVehicle(string memory _name, string memory _owner, string memory _number, string memory _license) public {
        vehicles.push(
            Vehicle(_name, _owner, _number, _license)
        );
        emit VehicleAdded(_name, _owner, _number, _license);
    }

    function getVehicles() public view returns (Vehicle[] memory) {
        return vehicles;
    }
}

It seems that your snippet is using a deprecated version of Solidity (I'm guessing 0.4), and I wrote the example using the latest version 0.8. There are some slight syntax changes between those versions, so just keep that in mind.

黯淡〆 2025-02-12 18:03:58

您的坚固性代码仅接受车辆的一个输入。要保留所有添加所有车辆的列表,您可以将它们添加到数组中。

这是一个(尽管是粗略的)例子。

contract VANET {
Vehicle[] vehicles;

struct Vehicle{
    string name;
    string owner; 
    string number; 
    string license;
}

event VehicleEvent(
    string name,
    string owner,
    string number,
    string license
);

function setVehicle(string memory _name, string memory _owner, string memory _number, string memory _license) public{
    vehicles.push(Vehicle(_name, _owner, _number, _license));
    emit VehicleEvent(_name, _owner, _number, _license);
}

function getVehicle(uint256 id) public view returns (string memory name, string memory owner, string memory number, string memory license) {
    return (vehicles[id].name, vehicles[id].owner, vehicles[id].number, vehicles[id].license);
}}

Your solidity codes only accepts one input of Vehicle. To keep a list of all vehicle added, you can add them into an array.

Here's an (albeit crude) example.

contract VANET {
Vehicle[] vehicles;

struct Vehicle{
    string name;
    string owner; 
    string number; 
    string license;
}

event VehicleEvent(
    string name,
    string owner,
    string number,
    string license
);

function setVehicle(string memory _name, string memory _owner, string memory _number, string memory _license) public{
    vehicles.push(Vehicle(_name, _owner, _number, _license));
    emit VehicleEvent(_name, _owner, _number, _license);
}

function getVehicle(uint256 id) public view returns (string memory name, string memory owner, string memory number, string memory license) {
    return (vehicles[id].name, vehicles[id].owner, vehicles[id].number, vehicles[id].license);
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文