I'd like my firebase web app to upload a file, and I'd like to have a cloud function that discovers this file and creates a database object based on its contents. Here's the last variant of many I've tried...
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { Storage } = require('@google-cloud/storage');
admin.initializeApp();
const db = admin.firestore();
exports.onStorageFinalize = functions.storage.object().onFinalize((object) => {
const storage = new Storage();
const bucket = storage.bucket('my app's storage bucket name');
return bucket.file(object.name).download(function (err, contents) {
// problem: no err, no contents, set doesn't run just a warning about promises!
const result = `got err=${err}, contents=${contents}`
return db.collection('myCollection').doc().set({
result: result
})
});
});
The very small file is uploaded, and this trigger runs. When I don't attempt getting the contents of the file, the database object is created fine (with a literal string instead of the file result).
But when I run it as shown above, first I get a warning that says:
onStorageFinalize: Function returned undefined, expected Promise or value
That's wrong. The doc for file().download() says it returns a promise.... and, more importantly, the database set() doesn't run.
I am astounded by how confusing and obscure (and I think wrong?) the google docs are on this seemingly mainstream use case. Much obliged if someone can help.