Ask Question
I麓m trying to write to firestore from a onCall firebase function.
My purpose is to update the client when something is written in firestore.
fireApp.firestore().collection('queries').doc(this.getUser.uid).onSnapshot(snap => {
snap.exists ?
snap.docChanges().forEach(async change => {
if (change.type === "modified") {
Would exist any other alternative to update client side from function progress?
node:
node v 12
firebase-functions:
"firebase-functions": "^2.3.0"
firebase-tools:
firebase tools --version
6.10.0
firebase-admin:
"firebase-admin": "~7.0.0",
let getLinks = fireApp.functions().httpsCallable('getLinks')
getLinks({
query: this.query.string,
limit: this.query.limit,
country: this.query.country,
uid: this.getUser.uid
}).then(result => {
So I麓m trying to write to firestore from this onCall function with this code
```const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
console.log('initialing functions at ' , new Date().toString())
exports.getLinks = functions.runWith({ timeoutSeconds: 540 }).https.onCall( (data,context) => {
console.log('starting to get links ' , new Date().toString())
console.log('data' , data.query, data.limit, data.country, data.uid)
console.log('context auth', context.auth, 'context.auth.uid', context.auth.uid)
// is there anything like admin.setCredentials(context.auth) necessary here?
const queries = admin.firestore().collection('queries');
let uid = data.uid
console.log('uid', uid);
console.log('queries ref', queries)
//probably when trying to write here is not being allowed
queries.doc(uid).set({LinksArrayLength: 'starting'})
.then( r => console.log('writing to firestore 1 result', r))
.catch( err => console.error('writing to firestore 2 error', err))
```
Should create the respective document in the queries collection
writing to firestore 2 error Error: Unexpected error determining execution environment: Invalid response from metadata service: incorrect Metadata-Flavor header.
at GoogleAuth.(H:\nprojetos\whats_app_sender\firebase_sender\vue_sender\wa_sender\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:164:23)
at Generator.throw ()
at rejected (H:\nprojetos\whats_app_sender\firebase_sender\vue_sender\wa_sender\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:20:65)
at processTicksAndRejections (internal/process/task_queues.js:89:5)
didn麓t try to deploy function yet.. I麓m running it locally through the firebase serve --only functions command
I found a few problems with this issue:
Solved
It required propper initialization
const credential = require('./xxxxx.json')
admin.initializeApp({credential: admin.credential.cert(credential), databaseURL: "https://xxx.xxx.firebaseio.com"});
this link (https://www.youtube.com/watch?v=Z87OZtIYC_0)
explains how to initialize it properly
my unexperienced with the sdk yet...
Thanks for the detailed report and sharing your solution @adrielwerlich. For future issues like these, StackOverflow is usually the best resource as you can get faster help there :) glad you figured it out!
adriel's video link worked perfectly to solve my issue
Most helpful comment
Solved
It required propper initialization
const credential = require('./xxxxx.json')
admin.initializeApp({credential: admin.credential.cert(credential), databaseURL: "https://xxx.xxx.firebaseio.com"});
this link (https://www.youtube.com/watch?v=Z87OZtIYC_0)
explains how to initialize it properly
my unexperienced with the sdk yet...