HI, i am taking the course ‘react native part 2’ and for the backend i downloaded the files everything worked till chapter ‘14 - posting data’. I have the following error from the server:
Unexpected token n in JSON at position 0
when i post normal json data from the react native it works , but when I use formData() with images it just doesn’t anymore… I am not able to solve this it is a bit frustrating … the code is just like moshe’s i checked line by line. I looked so much all i know is that apisauce may not support multipart/form-data but in the video the post works without any issues. Please is you know what could be happening it will be a great help.
import apiClient from ‘./client’;
const endpoint = ‘/listings’;
const getListings = () => apiClient.get(endpoint);
const addListing = (listing) => {
console.log(‘:: image ::’, listing.images[0]);
const data = new FormData();
data.append(‘title’, listing.title);
data.append(‘price’, listing.price);
data.append(‘categoryId’, listing.category.value);
data.append(‘description’, listing.description);
listing.images.forEach((image, index) =>
data.append('images', {
name: 'image' + index,
type: 'image/jpeg',
uri: image
})
);
if (listing.location)
data.append('location', JSON.stringify(listing.location));
return apiClient.post(endpoint, data);
// return apiClient.post(endpoint, {
// title: 'Test',
// price: '23',
// categoryId: 4,
// description: 'Test',
// location: {
// latitude: 37.33233141,
// longitude: -122.0312186
// }
// });
};
export default {
addListing,
getListings
};
the above code is exactly like the one in the video.