I am trying to upload an image from my react native app to the nodejs and to the cloudinary cloud, but its not working for me. Thats my react-native code:
const App = () => {
const [profileImageSource ,setProfileImageSource] = useState(require('./images/profilePic.png'));
const [imageData, setImageData] = useState("");
const [okay, setOkay] = useState(false);
const config = {
withCredentials: true,
baseURL: 'http://localhost:3000/',
headers: {
"Content-Type": "application/json",
},
};
const handleProfileImage = () => {
const options = {
title: 'Select photo',
base64: true
};
ImagePicker.showImagePicker(options, (res) => {
if(res.didCancel){
}
else if(res.error){
}
else{
const image = {data: res.data};
axios
.post('/clients/upload-image', {
data: JSON.stringify({image})
},
config
)
.then((res) => console.log("ok"))
.catch((err) => alert(err));
}
})
}
And that's my nodesjs code:
require('dotenv').config();
const cloudinary = require('cloudinary').v2;
//Cloudinary configurations
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});
app.post('/clients/upload-image', (req, res) => {
const imageStr = req.body.data;
const uploadedResponse =
cloudinary
.uploader
.upload(imageStr, {
upload_preset: 'user',
})
.then((uploadedResponse) => console.log(uploadedResponse))
.catch((err) => console.log("error"));
}
Where am I wrong? I keep getting errors and the pictures are not uploaded to the cloudinary