I am not receiving data on the server side. Sometimes I get data as an object. I have tried different ways but the image upload gets failed. Below is my code. I have tried different method but the result was the same image folder remain empty as am using multer middleware
Image is getting displayed using this snippet
<TouchableHighlight
style={[
styles.profileImgContainer,
{ borderColor: "#4632a1", borderWidth: 1 },
]}
onPress={openImagePickerAsync}
>
<Image source={{ uri: selectedImage.localUri }} style={styles.thumbnail} />
</TouchableHighlight>
This is the Image Picker Section
function PickImage() {
let [selectedImage, setSelectedImage] = useState("");
let openImagePickerAsync = async () => {
let permissionResult =
await ImagePicker.requestMediaLibraryPermissionsAsync();
if (permissionResult.granted === false) {
alert("Permission to access camera roll is required!");
return;
}
let pickerResult = await ImagePicker.launchImageLibraryAsync();
if (pickerResult.cancelled === true) {
return;
}
setSelectedImage({ localUri: pickerResult.uri });
};
}
Fetch API Call
async function upload() {
const data = new FormData();
data.append("image", {
uri: selectedImage.uri,
name: selectedImage.title,
type: selectedImage.type,
});
const setting = {
method: "POST",
headers: {
"Content-Type": "multipart/form-data;",
},
body: data,
};
try {
const fetchResponse = await fetch(url, setting);
const data = await fetchResponse.json();
alert(data);
} catch (e) {
alert(e);
}
}
Server-Side Code
app.post("/users", upload.single("image"), async (req, res) => {
console.log(req.body.file);
console.log(req.body);
const img = req.body.image;
if (!img) {
console.log("no image");
}
res.send({ congrats: "data recieved" });
});