简体   繁体   中英

React JS + REDUX + JWT Authentication JSON issue

I have tried a lot to get this done but i dont know why the json is not printing on the DIV tag

I am getting a user1 is undefined

i am using my own api to fetch data which is populate in users.

{ success: true, user: "Normal User" }

JSON is as above

got the authorized token and logged in with it successfully but when i get the JSOn i cant show the user in the HTML page

please help!

Thanks in advance

this is my HomePage.jsx

import { Link } from 'react-router-dom';
import { connect } from 'react-redux';

import { userActions } from '../_actions';

class HomePage extends React.Component {
    componentDidMount() {
        this.props.dispatch(userActions.getAll());
    }
    render() {
        var { user, users1 } = this.props;
        console.log(user); // token
        console.log(users1); // json with names and all
        return (
            <div className="col-md-6 col-md-offset-3">
                <h1>Hi {users1.user} !</h1>
                <p>
                    <Link to="/login">Logout</Link>
                </p>
            </div>
        );
    }
}

function mapStateToProps(state) {
    const { users, authentication } = state;
    const { user } = authentication;
    // var users1 = {"success":true,"user":"Normal User"}
    var users1 = users.users
    return {
        user,
        users1
    };
}

const connectedHomePage = connect(mapStateToProps)(HomePage);
export { connectedHomePage as HomePage };```

Looks like you didn't parse JSON after the fetch and users1 in your case is a string. Try JSON.parse(users1) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM