Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write data to firestore through firebase functions?

I can quite easily find how to add triggers for firestore events but I can't find any documentation on how to use firebase functions to write to firestore.

For example I have the following:

export const listener = functions.auth.user().onCreate((x)=>{
    //firestore function to add an entry for user with fields...
})

There doesn't seem to be a way to do this? Is it that the logic for adding data to firestore should be written client side? If so wouldn't that mean I would need to re-write that functionality for every platform?

like image 957
meds Avatar asked Dec 09 '17 06:12

meds


People also ask

What is Firestore functions?

Cloud Firestore function triggersfirestore object that allows you to create handlers tied to specific Cloud Firestore events. Event Type. Trigger. onCreate. Triggered when a document is written to for the first time.


1 Answers

You can use the Firebase Admin SDK to read and write Firestore in the same way you'd use it to read and write Realtime Database. There are plenty of official examples, especially this quickstart.

const admin = require('firebase-admin');
admin.initializeApp();

admin.firestore().collection('messages').add({original: original}).then(writeResult => {
    // write is complete here
});
like image 158
Doug Stevenson Avatar answered Oct 19 '22 05:10

Doug Stevenson