Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

React Native: Backend retrieves 'undefined' when uploading images

$
0
0

I try to upload images to my backend server, but I'm doing something wrong the api call.

Used Packages...

"react-native-image-picker": "^4.7.3","form-data": "^4.0.0","axios": "^0.21.4","react-native": "0.67.3"......

Where I pick the Images from the Gallery:

import {  launchImageLibrary,} from 'react-native-image-picker';import {uploadProfileImages} from '../services/apiService';export function MyRNApp() {let image1: any,    image2: any,    image3: any,    image4: any,    image5: any = null;launchImageLibrary(options, response => {              if (response.didCancel) {                console.log('User cancelled image picker');              } else if (response.errorCode) {                console.log('ImagePicker Error: ', response.errorCode);              } else if (response.assets) {                  image1 = {                    uri: response.assets[0].uri,                    type: response.assets[0].type,                    name: response.assets[0].fileName,                  };                  image2 = {                    uri: response.assets[1].uri,                    type: response.assets[1].type,                    name: response.assets[1].fileName,                  };                  image3 = {                    uri: response.assets[2].uri,                    type: response.assets[2].type,                    name: response.assets[2].fileName,                  };                  image4 = {                    uri: response.assets[3].uri,                    type: response.assets[3].type,                    name: response.assets[3].fileName,                  };                  image5 = {                    uri: response.assets[4].uri,                    type: response.assets[4].type,                    name: response.assets[4].fileName,                  };              }            });const registerComplete = async () => {      setLoading(true);      console.log('Before upload: '+ image1);      await uploadProfileImages(image1, image2, image3, image4, image5)        .then(async res => {          console.log('images uploaded...');          console.log('Result: ', res);        })        .finally(() => {          setLoading(false);        });  };}

My actuall axios call:

import FormData from 'form-data';export async function uploadProfileImages(  image1: any,  image2: any,  image3: any,  image4: any,  image5: any,): Promise<any> {  const formData = new FormData();  formData.append('images', image1);  formData.append('images', image2);  formData.append('images', image3);  formData.append('images', image4);  formData.append('images', image5);  return await axios({    method: 'POST',    url: `${API_URL}upload`,    data: formData,    headers: {'Content-Type': 'multipart/form-data',      Accept: 'application/json','Access-Control-Allow-Origin': '*',      Authorization: AUTH_HEADER,    },  });}

NestJs Backend:

@Post('upload')  @HttpCode(200)  @UseInterceptors(    FilesInterceptor('images', 5, {      dest: '../uploads',      fileFilter: (req, file, cb) => {        if (file.mimetype.startsWith('image')) {          cb(null, true);        } else {          cb(new Error('No supported filetype'), false);        }      },    }),  )  async uploadFile(    @UploadedFiles() images: Array<Express.Multer.File>,    @Request() req: any,  ): Promise<any> {    if (req.headers['authorization'] === process.env.UPLOAD_KEY) {>there it says 'undefined' when making call from RN App 🧐      console.log('Uploaded files: '+ images[0]);      return { files: images };    } else {      return { status: 'failed' };    }  }

The Backend API works fine (Tested in Postman).But when I'm making the api call in the RN App, it seems like the actuall photo data will not be send.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>