Sebastian
Sebastian

Reputation: 13

Accessing data returned from promise in Redux

I'm trying to access the data of the first dataElement in the array. How can I reach it? I want to console.log it's name.

import React, { Component } from 'react';

class Submit extends Component {
    componentDidMount() {
        const programStage = this.props.getProgramStage();

        if (programStage !== null) {
            console.log('Stage loaded...');
        }

        console.log(this.props.getForm());
    }

    render() {
        return <div />;
    }
}

export default Submit;

How the console looks like

Upvotes: 1

Views: 126

Answers (2)

rsmidt
rsmidt

Reputation: 79

It seems that the return type of the call getForm() is a Promise (according to the output). You would need to append a handler via the then method of the promise to actually get the value you are looking for.

E.g.

componentDidMount() {
  ...

  this.props.getForm().then(result => console.log(result))
}

Upvotes: 0

Easwar
Easwar

Reputation: 5422

As shown in the pic, the promise is resolved. Hence you should be able to access the data like :

this.props.getForm().then((data) => console.log(data[0].name))

Upvotes: 1

Related Questions