I am trying to create a cloud function that stores a file into storage. Oringally I had this on the front-end, but have decieded that I need this done as a cloud function. I am struggling with how to do this. my function looks like:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports.exampleFunction = functions.https
.onCall((data, context) => {
const someData = {
some1:"abc",
some2: "def"
};
let newKey;
return admin.database()
.ref("/level1/"+data.dataVar1+"/level2/"+data.dataVar2+"/random")
.push(someData)
.then((snapshot)=>{
newKey=snapshot.key;
return admin.database()
.ref("/level1/"+data.dataVar1+"/level2/"+data.dataVar2)
.once("value");
})
.then((snapshot)=>{
const file = data.file;
//Error here
return admin.storage().ref()
.child(snapshot.val().randomVar+"/"+newKey)
.put(file);
})
.catch((error)=>{
console.log(error);
console.log(error.message);
return error;
});
});
However, when I run this I get the following error:
admin.storage(...).ref is not a function
How can I upload the file to storage?