Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Object Promise to String in Javascript

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} />
}

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
    );
}

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

like image 207
Saensation Avatar asked Jul 09 '18 16:07

Saensation


People also ask

How do I turn a promise object into a String?

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.

How do you turn an object into a String in JavaScript?

Stringify a JavaScript ObjectUse the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

What is object promise in JavaScript?

What is a Promise in JavaScript? A Promise is a special JavaScript object. It produces a value after an asynchronous (aka, async) operation completes successfully, or an error if it does not complete successfully due to time out, network error, and so on.

How do I return a value from Promise TypeScript?

To access the value of a promise in TypeScript, call the then() method on the promise, e.g. p. then(value => console. log(value)) . The then() method takes a function, which is passed the resolved value as a parameter.


1 Answers

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.

like image 104
Saensation Avatar answered Oct 23 '22 03:10

Saensation