在Javascript中将对象承诺转换为字符串 [英] Converting Object Promise to String in Javascript

查看:163
本文介绍了在Javascript中将对象承诺转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React,Next.Js,semantic-ui-react和Solidity.我的目标是打印出用户地址(从MetaMask)和ProjectTitle(由用户设置)作为语义UI反应卡的元信息.可以打印出页眉"中的地址,但是我无法将ProjectTitle打印为元".标题应该是一个字符串,但是我收到一个对象承诺.

I'm working with React, Next.Js, semantic-ui-react and Solidity. It is my goal to print out the users address (from MetaMask) and a ProjectTitle (set by User) as meta infomation for a semantic-ui-react card. To print out the address in the 'header' is working, but I'm not able to print out the ProjectTitle as 'meta'. The Title should be a String but I'm receiving a Object Promise.

static async getInitialProps() {
    const projects = await factory.methods.getDeployedProjects().call();
    return {
        projects
    };
}

async getProjectTitle(address) {
    let title;
    try {
        title = await factory.methods.projectTitle(address).call();
    } catch (err) {
        console.log('err');
    }
    return title;
}

renderProjects() {
    const items = this.props.projects.map(address => {
        return {
            header: address,
            color: 'green',
            description: (
                <Link route={`/projects/${address}`}>
                    <a>View Project</a>
                </Link>
            ),
            **meta: this.getProjectTitle(address)**,
            fluid: true,
            style: { overflowWrap: 'break-word' }
        };
    }, );
    return <Card.Group items={items} />
}

团结合同的一部分:

address[] public deployedProjects;
mapping(address => string) public projectTitle;

function createProject(string startup, string title, string deadline, string description, uint wage) public {
    address newProject = new Project(startup, title, deadline, description, wage, msg.sender);
    projectTitle[newProject] = title;
    deployedProjects.push(newProject);
}

function getDeployedProjects() public view returns (address[]) {
    return (
        deployedProjects
    );
}

基本框架来自Stephen Grider的Udemy课程以太坊和扎实性:完整的开发人员指南".

The basic framework is from the Udemy Course "Ethereum and Solidity: The Complete Developer's Guide" by Stephen Grider.

推荐答案

没有直接的方法可以将对象Promise转换为字符串. 继续处理的唯一方法是调用await函数或使用.then()和回调函数.

There is no direct way to convert an Object Promise into a String. The only way to continue processing is to call an await function or use .then() and a callback function.

这篇关于在Javascript中将对象承诺转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆