firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Issue reading a file in a JSON object

Open qng5150 opened this issue 6 years ago • 1 comments

Related issues

https://github.com/firebase/firebase-functions/issues/141

[REQUIRED] Version info

Node: v10.22.0 client library js: firebase": 7.16.1 firebase-admin: 8.10.0, firebase-functions: 3.11.0, firebase-tools: 8.7.0

[REQUIRED] Test case

firebase/firebase.js

import firebase from 'firebase';
import 'firebase/auth';
import 'firebase/firebase-functions';
const firebaseConfig = {
    apiKey: 
    authDomain: 
    databaseURL: 
    projectId: 
    messagingSenderId:
    appId:
    measurementId: 
};

// Initialize Firebase
const fire = firebase.initializeApp(firebaseConfig);
export const analytics = fire.analytics();
export const auth = fire.auth();
export const functions = fire.functions('australia-southeast1');

client code

import {functions} from "../../firebase/firebase";

    const designVerification = functions.httpsCallable('addMessage');
    const onSubmit = async (values) => {
        try {
            console.log(values);
              addMessage(serialize(values).then(function(result) {
                    // Read result of the Cloud Function.
                    let sanitizedMessage = result;
                    console.log(result);
                }).catch((err) => {
                    console.log(err.message);
                });
            }
    };

   <input className="file-input" type="file" name="testReport"
                                                   onChange={onTestReportChange}
                                                   ref={register({required: true})}/>

server code

exports.designVerification = functions
    .region('australia-southeast1')
    .runWith({timeoutSeconds: 360 })
    .https.onRequest(async (req, res) => {
        res.set('Access-Control-Allow-Origin', '*');
        if (req.method === 'OPTIONS') {
            // Send response to OPTIONS requests
            res.set('Access-Control-Allow-Methods', 'GET');
            res.set('Access-Control-Allow-Headers', 'Content-Type');
            res.set('Access-Control-Max-Age', '3600');
            res.status(204).send('');
        } else {
            console.log('Here I am');
            console.log(req.body);
            res.status(200).send('');
       }
);

[REQUIRED] Steps to reproduce

Submit a file in the values as a JSON with a file in it.

[REQUIRED] Expected behavior

Expect the file to be in the req.body

[REQUIRED] Actual behavior

no file in the req.body. Have also tried busBoy and unable to parse the req json has an empty array

{ testReport: {0: {}}

on the client side have verified that the file has been set in the json

{ testReport: FileList
0: File {name: "Artisan Capability Statement(2).pdf", lastModified: 1600852844603, lastModifiedDate: Wed Sep 23 2020 19:20:44 GMT+1000 (Australian Eastern Standard Time), webkitRelativePath: "", size: 189898, …}
length: 1
}

How am I suppose to get the file? busBoy doesn't work complains about not having multipar-form. There should be a way of sending the json using the client library


### Were you able to successfully deploy your functions?

No Error

qng5150 avatar Sep 26 '20 23:09 qng5150

I got around this by not using the client library and using http request instead.

  const formData = new FormData();
  formData.append('testReport', testReport);
  fetch('https://cloudfn-url', {
                    method: 'POST',
                    body: formData
                })
                    .then(result => {
                        console.log('Success:', result);
                    })
                    .catch(error => {
                        console.error('Error:', error);
                    });

qng5150 avatar Sep 28 '20 13:09 qng5150