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...