Accessing data from rest api with reactjs

Viewed 1116

I´m developing an application with react and I found a big trouble manipulating the information from a rest api.

I have a fetch where the api by get method should bring me the customer id, but I always get an error like this.

{error: "Customer not found"} error : "Customer not found"

Image of the error

I want to get an array with only the id of all customers, to do that I make this fetch function, but I dont know what to put in .then((resposneValue) to get the correct array. I have tried with map function but then I get an error like map prototiping is not definded.

here is my code:

getHistory() {
    const {customer} = this.state
    fetch(
        DOMAIN+'/api/customers/'+customer._id, {
            method: 'get',
            dataType: 'json',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization':'Bearer '+this.props.token
            }
        })
        .then((response) =>
        {
            return response.json();
        })
        .then((responseValue) => {
            /*responseValue.map(function (v) {
                v.platform = v.app ? v.app.platform : null  })*/
            this.setState({customsId:responseValue});
            console.log(responseValue);
        })

}

Edit:

I have another fetch:

getCustomers(){

    fetch(

        DOMAIN+'/api/customers/shop/'+this.props.salon, {
            method: 'get',
            dataType: 'json',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization':'Bearer '+this.props.token
            }
        })
        .then((response) =>
        {
            console.log(this.props.salon);
            return response.json();
        })
        .then((responseData) => {
            responseData.map(function (v) {
                v.platform = v.app ? v.app.platform : null  })
            this.setState({customers:responseData});
           //console.log(responseData);

        })
        .catch(function() {
            console.log("error");
        });
}

The console.log inside the then function report me this array:

(22) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{_id: "591d66f69ea5d935ed4b9ba9", shop: {…}, email:...

Array with the customer _id

So the question is, how can I access that id value with the first fetch or second fetch?

2 Answers
Related