0
votes

I am using firebase functions and I want to create a function which will get a base64 image and will save it into the storage. I did some research and apparently I am supposed to use something like firebase.storage().ref()

The problem is I don't know what object this 'firebase' is, and how I can i get it? For all of my other functions I used 'firebase-functions' and 'firebase-admin'. I managed to upload the image using the admin but its uploaded badly and I can't get the image back later.

Here is the code I used for it:

export const uploadImage = functions.runWith(runtimeOpts).https.onRequest(async (req, res) => {
const bucket = admin.storage().bucket()
const file = bucket.file(`users/${email}`);
file.save("my base64 image", {
   contentType: "image/jpeg"
        }, (err) => {
            if (err) {
                throw err;
            } else {
                functions.logger.debug("NO WAY!")
            }
        })
}
1

1 Answers

1
votes

CLI:

npm install --save firebase

CODE:

// DO
var firebase = require("firebase/app");
require("firebase/firestore"); 

You also may require the complete firebase SDK with all its modules, see below. But it is better to include only the modules you acutally need.

// DON'T
var firebase = require("firebase");

Further info:

https://firebase.google.com/docs/web/setup#node.js-apps

Example code:

https://groups.google.com/g/firebase-talk/c/aDJvYyNIJik/m/UvYGZeyaBgAJ