google-cloud-functions

Firebase count num children of parent

梦想的初衷 提交于 2020-01-21 08:55:47
问题 I'm trying to get the number of children for a parent node in js firebase. I'd like to have: 'user': { '-Yuna99s993m': { count: 1}, '-Yada99s993m': { count: 2}, } I'm creating a cloud function witch every time a new node is entered it should add count equal to numChildren of user node. exports.setCount = functions.database.ref('/user/{userId}').onWrite(event => { // This doesn't work const count = event.data.ref.parent.numChildren(); return event.data.ref.update({ count }); }); Any help to

How to delete a file using Cloud Functions?

不打扰是莪最后的温柔 提交于 2020-01-21 01:11:05
问题 When I delete a post in my Firebase database I want a cloud function to delete the post's thumbnail in firebase storage accordingly. My issue is when I'm trying to delete the thumbnail I don't think I'm locating the image file correctly. Here is what I have tried: const functions = require('firebase-functions') const admin = require('firebase-admin') const gcs = require('@google-cloud/storage')() exports.deletePost = functions.database.ref('Posts/{pushId}').onWrite(event => { const original =

ESLint error trying to deploy functions Firebase

喜欢而已 提交于 2020-01-20 04:09:08
问题 I'm try to deploy fireabase example , but when I try to deploy it , CLI launches an error: [CODE] const functions = require('firebase-functions'); //to activate firebase functions const admin = require('firebase-admin'); //to active firebase database permissions admin.initializeApp(functions.config().firebase); exports.addMessage = functions.https.onRequest((req, res) => { // [END addMessageTrigger] // Grab the text parameter. const original = req.query.text; // [START adminSdkPush] // Push

Cloud Functions for Firebase: can you detect app uninstall?

泄露秘密 提交于 2020-01-20 02:48:28
问题 Is it possible to trigger a Cloud Function when the user uninstalls the app, so that we can clean up the anonymous user realtime database entry? 回答1: You can detect app uninstall for Android as an automatically collected Analytics event called app_remove . Then you could trigger a Cloud Function to run when that event occurs. You would also need to use the Firebase Admin SDK to access the database. Check out some of the Cloud Functions for Firebase GitHub samples to see examples of using

How to use Admin SDK with limited privileges on Firestore?

佐手、 提交于 2020-01-19 05:37:45
问题 I have some trouble with Cloud function and firestore rules. I would like use cloud function with limited privilèges on Firestore and give only has access as defined in the Security Rules It's working without problem on RTDB but not on Firestore. I have try with this rules service cloud.firestore { match /databases/{database}/documents { match /init/{ID=**} { allow read, write: if true; } match /test/{ID=**} { allow read, write: if false; } } } And this const admin = require('firebase-admin')

How to get an email when new key is created in Firebase real time database

痞子三分冷 提交于 2020-01-17 17:27:33
问题 Well, I have looked almost everywhere, and apparently there is Firebase Cloud Functions which can be used to send me an email whenever a new key is created in my database. However, I cannot wrap my head around where to start and how to do it. Can someone help me please. 回答1: You can indeed use a Cloud Function to send an email each time a new node (i.e. a new key) is created in the database. Have a look at the documentation for Realtime Database triggers here and at one of the official Cloud

Can't Find file after having written to it in Google Cloud Functions

∥☆過路亽.° 提交于 2020-01-17 01:37:38
问题 When I write to os.tmpdir I can see the file when I look into the directory but on subsequent calls to same function I see nothing. I assume this means the file somehow no longer exists (unlikely) or I am looking in a different place. I know I should be deleting files in the tmp directory after I use them but before that I want to be able to find the files again to be assured that any deletion methods are working at a later stage. below is the code I user to write the file. var wstream = fs

How to query the existing children of a sibling node and loop over them to detect if it exists?

十年热恋 提交于 2020-01-16 20:55:30
问题 I am trying to update (adding) new children on a firebase database. On this scenario I am updating two nodes and_"door/{MACaddress_1}/ins"_ & _"doors/{MACaddress_2}/ins"_ and both of this nodes need to write through a Google Cloud function to a sibling node called "rooms/{roomPushKey}/ins" . I will be updating the initial "doors" nodes through an Arduino program on a IoT device with a unique MAC Address that is connecting to the database to the '/doors/' + {MAC Address} + '/ins'. These are

Firebase functions callable - image as argument

泄露秘密 提交于 2020-01-16 18:58:37
问题 I have a firebase function which given some arguments creates a "post" with a title and the such. Is there a way to send an image as an argument which can then be processed by firebase functions and then upload to firebase storage. I think it is possible by decoding the file/image object to a base64 string which could be then sent as an argument. How would I convert the file object from html file input to base64? On the other side how would firebase functions know that the string is an actual

How to update a collection using cloud function?

烂漫一生 提交于 2020-01-16 18:56:52
问题 I would like to update the score using a cloud function I've tried this so far exports.rate = functions.https.onRequest((request, response) => { admin .firestore() .collection() .where("index", "==", request.index) .get() .then(snap => { snap.forEach(x => { const newRating = x.data().score + request.value; firebase .firestore() .collection() .doc(x.id) .update({ score: newRating }); }); }); }); 回答1: The following should work: exports.rate = functions.https.onRequest((request, response) => {