1

Usecase: I need to upload an .xlsx file to a cloud function API parse the data to json form and upload it to Firebase Firestore. I am using typescript for writing cloud functions.

I read about multer and came to the understanding that using multer with firebase has some issues. Busboy was the other option.

And went through this example code : https://gist.github.com/tonkla/5e893aa8776923ad6a2c9c6b7c432f3d

The headers['content-type'] I am sending to busboy is 'application/x-www-form-urlencoded'

and tried to imply it in my code.

To check the file upload I used postman to upload the file to my url but its going into request timed out.

Postman Screenshot

export const fileUpload = functions.https.onRequest(async (req, res) => {
    const data = await parseMultipartFormData(req.rawBody,req.headers['content-type'])
    console.log("Data from busboy:"+data)
    const workbook = xlsx.read(data,{type: 'buffer'})
    const jsonRows = xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]])
    res.status(200).json(jsonRows)

 });

 function parseMultipartFormData(rawBody, headers) {
    return new Promise((resolve, reject) => {
      const buffers : any[]= []
      const busboy = new busboyMain({
        headers: { 'content-type': headers },
      })
      busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
        file.on('data', data => {
          buffers.push(data);
        })
        file.on('end', () => {
          resolve(Buffer.concat(buffers));
        })
      })
      busboy.on('error', error => reject(error))
      busboy.end(rawBody)
    })
  }
  • Could you please provide details on what have you tried so far and what you are facing issues to do it? As per the documentation [How to Ask](https://stackoverflow.com/help/how-to-ask), please, add more information, so it's easier to understand what you want and you have done so far, so the Community can help you. Code samples, of your Cloud Functions, how you are adding to Firestore, etc., will help to understand where you are. – gso_gabriel Aug 12 '20 at 10:25
  • @gso_gabriel I have updated the question please kindly look into it. – ayush singh Aug 13 '20 at 23:57
  • It seems that you are facing a `timeout` when sending the file to this parser. I have found this other [here](https://stackoverflow.com/questions/30859901/parse-xlsx-with-node-and-create-json) parser for JS to parse XLSX to JSON. Could you please take a look at it? It seems to be simpler that this other one. – gso_gabriel Aug 14 '20 at 09:36
  • @gso_gabriel I am logging out data after calling the busboy function, but its not been logged. So I think maybe that the request is getting timed out during some busboy process. – ayush singh Aug 15 '20 at 04:54
  • Hi @ayushsingh yes, probably ti's related during the `busboy` process. Have you give it a shot to the other parser that I indicated or have tried to contact the developers of `busboy`? – gso_gabriel Aug 20 '20 at 09:28

0 Answers0