返回固体中的阵列或对象列表
我是牢固性的新手,并尝试在我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当您调用
setVehicle()
时,重写存储属性的值值。因此,将所有这些值分组为
struct
(类型类型),然后将这些结构放在数组中,以便您可以保留所有输入值。然后,您可以简单地检索整个数组:看来您的摘要正在使用固定的固定版本(我猜是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: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.
您的坚固性代码仅接受车辆的一个输入。要保留所有添加所有车辆的列表,您可以将它们添加到数组中。
这是一个(尽管是粗略的)例子。
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.