Benjith Mathew
Benjith Mathew

Reputation: 1221

TypeError:expected dynamic type 'string' but had type 'object' 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

Upvotes: 3

Views: 2552

Answers (1)

lancelot lan
lancelot lan

Reputation: 46

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.

Upvotes: 3

Related Questions