我正在使用React,Next.Js,semantic-ui-react和Solidity。我的目标是打印出用户地址(从MetaMask)和ProjectTitle(由用户设置)作为语义UI反应卡的元信息。可以打印出“页眉”中的地址,但是我无法将ProjectTitle打印为“元”。标题应该是一个字符串,但是我收到一个对象承诺。
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课程“以太坊和团结:完整的开发人员指南”。
没有直接的方法将对象承诺转换为字符串。继续处理的唯一方法是调用一个
await
函数或使用.then()
一个回调函数。