gpt4 book ai didi

javascript - 在 Javascript 中将对象 Promise 转换为字符串

转载 作者:行者123 更新时间:2023-12-03 13:29:24 25 4
gpt4 key购买 nike

我正在使用 React、Next.Js、semantic-ui-react 和 Solidity。我的目标是打印出用户地址(来自 MetaMask)和 ProjectTitle(由用户设置)作为语义 ui react 卡的元信息。打印“标题”中的地址是有效的,但我无法将 ProjectTitle 打印为“元”。标题应该是一个字符串,但我收到了一个对象 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 合约的一部分:

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 类(class)“以太坊和 Solidity:完整开发人员指南”。

最佳答案

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

关于javascript - 在 Javascript 中将对象 Promise 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com