I am using an onCall
firebase cloud function to check if a document exists in a collection on the firestore.
If the document exists, i want to continue, if it still does not exist, than i want to create it.
I have tried many different things, but always got errors, so now i am trying to go step-by-step and simply get the content of a file i know exists on the collection. The collection is called "timeline_state" and the document is called "888"
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.afterLogin = functions.https.onCall((data, context) => {
console.log("afterLogin STARTING");
const getDocument = admin.firestore().collection('timeline_state').doc('888').get();
return getDocument.then(doc => {
console.log(doc);
return doc
}).catch(error => {
console.log(error)
return error;
})
})
When i call it from my code i get an empty data
object,
and in the Firebase functions log i see the function runs but i get an error:
/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/index.js:28
async function request(opts) {
^^^^^^^^
SyntaxError: Unexpected token function
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/gcp-metadata/build/src/index.js:17:18)
Any ideas on what i am doing wrong, and maybe how i can fix it? Thanks!
To read a single document, we can use DocumentReference's get() method that returns a Task<DocumentSnapshot>, while reading multiple documents from a collection or Query, we can use Firestore Query's get() method that returns an object of type Task<QuerySnapshot>. Both methods read the data only once.
My solution, thanks to a tip i got, was to update the node engine to 8.
To do so, i added the following to the package.json
"engines": {
"node": "8"
}
Maybe this is helpful to someone else... ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With