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

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

问题描述

我正在使用 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} />
}

Solidity 合约的一部分:

Part of the Solidity Contract:

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 课程以太坊和 Solidity:完整的开发者指南".

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

推荐答案

没有直接的方法可以将 Object Promise 转换为 String.继续处理的唯一方法是调用 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 中将对象 Promise 转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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