Waleed Ahmad
Waleed Ahmad

Reputation: 3

Response returning undefined react js axios

I trying to view status of a variable (salesforecasting) in database. Its responds by sending true or false which is received fine in Postman. However when it gets to react, it is shows undefined on console. if i see whole response object, there i can see value has return correctly but when i try to print it, it shows undefined.

in routes/api/salesforecasting.js

router.post('/view', function(req, res) {
    const email="[email protected]"
    Customer.findOne({Email:email})
    .then(data=>{
        if(data){
            let salevalue=data.Salesforecasting
        res.send({
            value: salevalue 
        });
    }
    });
});

in react file

componentDidMount(){
            return axios.post('http://localhost:3000/api/sales-forecasting/view')
            .then(response => {
                //const output = response.value
                const value = response.value;
                {console.log(arr.value)}
                this.setState({
                    added: value
                });
            });
    }

Upvotes: 0

Views: 835

Answers (1)

Sm Srikanth
Sm Srikanth

Reputation: 2360

I think u need to check in response.data.value instead of response.value. axios populates the entire server response in response.data

Upvotes: 2

Related Questions