0

I'm Using React and nodejs I can successfully retrieve the data but I'm Unable to store it in a variable

 OnSubmit(e){
    
    const user={
      email:this.state.email,
      password:this.state.password,
      
    }
    axios.post('http://localhost:5000/Users/login',user)
    .then(res=>(console.log(res.data)))//want to store this res.data in a variable
    
    
    
    
    
    
    localStorage.setItem("token","got")
    
    this.setState({
      loggedin:true
    })

5 Answers 5

2

In arrow function, Paranethesis () has implicit return statement and can contain only single statement. Whereas, brace {} need explicit return statement and you can add multiple statements inside scope.

axios.post('http://localhost:5000/Users/login',user)
    .then(res=> {
       // Here, you can do required manipulation of data returned from promise
       console.log(res.data)
       
       localStorage.setItem("token","got")
    
       this.setState({ loggedin:true })
    })
Sign up to request clarification or add additional context in comments.

Comments

1

You can either same that to a variable or to your React state (class component) or to hooks. Multiple ways are available: class based component: axios.post('http://localhost:5000/Users/login',user) .then(res=>this.setState({data:res.data}))

Comments

1

Define a variable in state, say "userData: null"; then do this,

axios.post('http://localhost:5000/Users/login',user)
.then(res=>{
 // try to console.log `res` and checkout its hierarchy. 
 this.setState({userData: res.data});
})

Comments

1

Simply like this:

let userDetails = null;
axios.post('http://localhost:5000/Users/login',user)
    .then(res=>{userDetails = res.data})

but if you want to use it outside the async call, then the value of userDetails will be null, as this is an Async fetch, so you can set this value in the state inside .then() like this

axios.post('http://localhost:5000/Users/login',user)
    .then(res=>{this.setState({userDetails: res.data})})

4 Comments

I have saved data in a variable like this axios.post('localhost:5000/Users/login',user) .then(res=>{ Userdata=res.data.users console.log(Userdata) console.log(Userdata._id) }) But I'm unable to use it in the render() function if(this.state.loggedin){ return <Redirect to={/UI/Navbar/Dashboard/${Userdata._id}}/> } it gives me an error TypeError: Cannot read property '_id' of undefined
to use it in render, you should set this value in state as I mentioned in my answer, then use it like this: <Redirect to={/UI/Navbar/Dashboard/${this.state.userDetails._id}}/>
tried as you said but it still gives me the same error
try to log userDetails, maybe it doesn't have _id, or it's nested more
1

You can use this

axios.post('http://localhost:5000/Users/login',user)
    .then(res=>{this.setState({userDetails: res.data})})

1 Comment

Or no need for setting this in the state if you want to store this in local storage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.