-2

I work on an app on React-native, and i want to save all the user data on the server with request to an API. But when i tried, my request are buggy.

{"body": "{
    \"residenceId\": \"5d88dfb55feb4c06a5cfb756\",
    \"date\": \"2019-10-04T16:37:10.048Z\",
    \"espaces\": [
        {
            \"espaceId\": \"5d88dfb55feb4c06a5cfb761\",
            \"photosPath\": [
                \"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-2c97392b-fd7c-4480-9d9e-d055c74cab7e.jpg\"
            ],
            \"comment\": \"Un sol\"
        },
        {
            \"espaceId\": \"5d88dfb55feb4c06a5cfb760\",
            \"photosPath\": [
                \"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-0518bb83-7d66-43f5-a0df-4e803bca50da.jpg\",
                \"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-d416bd03-051f-43f0-8d09-478ad616562a.jpg\"
            ],
            \"comment\": \"Un plafond\"
        }
    ]
}"}

I don't know why.

I have try to put Stringify, i have try without stringify, whenever à try my "espaces" array have bug with stringify. I think my array is the heart of the bug. I have see many topic for bug like this, but nobody find my solution.

My method generate my object with all data (and change the architecture)

_saveAllData() {
        const token = this.props.token;
        let data = this.props.picturesAndComment;
        let date = new Date();

        let newData = {
            residenceId: this.props.residence._id,
            date: date,
            espaces: []
        };

        for (let i = 0, c = data.length; i < c; i++) {
            newData.espaces[i] = {
                espaceId: data[i].idSubspace,
                photosPath: [],
                comment: data[i].comment
            };

            for (let j = 0, d = data[i].pictures.length; j < d; j++) {
                newData.espaces[i].photosPath[j] = data[i].pictures[j].uri;
            }
        }
        console.log(newData);
        //this.setState({ isLoading: true });

        /*Promise.all([postVisit(token, newData), postPhoto(token, picturesDataForUpload)]).then((arrayOfResponses) => {
            console.warn(arrayOfResponses);
            this.setState({ isLoading: false });
        }).catch((error) => {
            console.warn('Error :', error);
            this.setState({ isLoading: false });
        });*/
    }
}

My API method for send data

export function postVisit(token, data) {
    const url = "http://51.91.11.5:8080/visites/";
    const request = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + token.accessToken
        },
        body: data
    };
    console.log(request);
    return fetch(url, request)
        .then((response) => response.json());
}

I expect a normal JSON without backslash everywhere...

MajorKurk
  • 26
  • 1
  • 5
  • 2
    How would you represent an `"` inside a string encapsulated with `"` then, if not escaped? – baao Oct 04 '19 at 17:25
  • Because is not a string, it's an Array... – MajorKurk Oct 04 '19 at 17:34
  • I would recommend first creating a [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) - get rid of all the extraneous information, that will help people get to the root of the issue faster – jonny Oct 04 '19 at 17:34
  • @MatthyDragneel it's a *string* you can see the double quotes in your output `"body": "{` starts here and ends at the very end with `}"` - the ENTIRE value of `body` is a string of JSON data. – VLAZ Oct 04 '19 at 17:37
  • When i console.log, the output is a string @VLAZ My console.log render every of my object like a JSON, but it's not a JSON, it's an object with array inner. And when i push my object in request, JSON parser send me an error for JSON parsing error. – MajorKurk Oct 04 '19 at 17:39
  • @MatthyDragneel *precisely* - it's a string, which contains double quotes `"`. The console represents a string as wrapped in double quotes, so how would it represent double quotes *inside* the string making sure they don't look like they are terminating the string? – VLAZ Oct 04 '19 at 17:41
  • Why all my object is a string ? It's okey for JSON ? – MajorKurk Oct 04 '19 at 17:48
  • JSON is always a string. Maybe your confusing an object with it.. – baao Oct 04 '19 at 18:37

1 Answers1

0

The problem is solved. The concern was my client's API rather than my code. Thank you for your answers.

MajorKurk
  • 26
  • 1
  • 5