首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从映射中读取一个uint继续返回一个随机地址。

从映射中读取一个uint继续返回一个随机地址。
EN

Ethereum用户
提问于 2016-05-21 09:40:56
回答 1查看 776关注 0票数 4

我是刚踏实的。在从映射中存储和检索数据的简单操作(uint=>地址)中,我面临一个问题。

我定义了一个简单的合同:

代码语言:javascript
复制
contract test {
    mapping (uint => address) public testmap;

    function add_to_map  (uint _key, address _val) public{
        testmap[_key] = _val;
    }
    function get_from_map(uint _key) returns (address){
        return testmap[_key];
    }   
}

在html中,我要做的是:

代码语言:javascript
复制
function createTestContract(){
    var test = testContract.at("0xc383dfb5fc71ff1bb2bbadb812229681fb7a8e3c");
    test.add_to_map(1, "0x12ca4a043753cd5537af99ad314a299962238ed2");       
    console.log('Key: 1'  + ' address: ' + test.get_from_map(1));
}

作为输出,我期望地址- 0x12ca4a043753cd5537af99ad314a299962238ed2,它存储在键1上,但是它在控制台日志中返回一些随机地址。

代码语言:javascript
复制
Key: 1 address: 0x07824f2f2a330dd2e260437af299800175dc2642a63c71a5f824cee54eae2264

我在AlethZero上使用私人连锁。感谢有人能帮忙。

EN

回答 1

Ethereum用户

回答已采纳

发布于 2016-05-21 09:51:33

您需要指定constant,以便获得call的返回值:

function get_from_map(uint _key) constant returns (address)

没有常量,test.get_from_map(1)是一个sendTransaction,它总是返回一个事务哈希。请参阅交易和呼叫之间有什么区别?

票数 7
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/4155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档