1

How do I save data to Firestore in Firebase Functions?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.saveContact = functions.https.onRequest((request, response) => {

    admin.firestore().collection("contact").add({
        name: request.name;
    }).then(function(docRef) {
        console.log("Document written with ID: ", docRef.id);
    }).catch(function(error) {
        console.error("Error adding document: ", error);
    });

    response.send("Hello from Firebase!");
});

With this code I'm getting this error

Error: could not handle the request

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
InfoStatus
  • 6,983
  • 9
  • 41
  • 53
  • Possible duplicate of [Cloud Functions for Firebase: 'Error: could not handle the request'](https://stackoverflow.com/questions/45600367/cloud-functions-for-firebase-error-could-not-handle-the-request) – Héctor Oct 31 '17 at 12:55
  • 2
    Are you also initializing the Admin SDK using `admin.initializeApp(functions.config().firebase);`? There will be a description of the error in the Firebase console under Functions which will better explain the issue. – Jen Person Oct 31 '17 at 13:34
  • Thank you Jen, that's what was missing. – InfoStatus Nov 01 '17 at 21:53

0 Answers0