Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError:expected dynamic type 'string' but had type 'object' React native

Tags:

react-native

I have to upload an image to cloudinary with react-native. I have used this code:

uploadImage(source) {
let timestamp = (Date.now() / 1000 | 0).toString();
let api_key = '***********'
let api_secret = '**************'
let cloud = '*****'
let hash_string = 'timestamp=' + timestamp + api_secret
let signature = CryptoJS.SHA1(hash_string).toString();
let upload_url = 'https://api.cloudinary.com/v1_1/' + cloud + '/image/upload'

let xhr = new XMLHttpRequest();
xhr.open('POST', upload_url);
xhr.onload = () => {
  console.log(xhr);
};
let formdata = new FormData();
formdata.append('file', {uri: source, type: 'image/png', name: 'upload.png'});
formdata.append('timestamp', timestamp);
formdata.append('api_key', api_key);
formdata.append('signature', signature);
xhr.send(formdata);};

But I get this error:

TypeError:expected dynamic type 'string' but had type 'object'

enter image description here

like image 353
Benjith binja Avatar asked Jan 31 '26 14:01

Benjith binja


1 Answers

If you got "source" from "react-native-image-picker", try to change

formdata.append('file', {uri: source, type: 'image/png', name: 'upload.png'}); 

into

 formdata.append('file', {uri: source.uri, type: 'image/png', name: 'upload.png'});

in android. I fixed my problem in this way.

like image 104
lancelot lan Avatar answered Feb 03 '26 06:02

lancelot lan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!