Reputation: 4159
Following the official firebase cloud functions tutorial (https://firebase.google.com/docs/functions/get-started), running into error when trying to actually deploy and use the function (https://firebase.google.com/docs/functions/get-started#deploy-and-execute-addmessage). After successfully completing deployment of the example function and instructed to "Add a text query parameter to the addMessage() URL, and open it in a browser", the result is that the text
Error: could not handle the request
appears in the browser. Inspecting the developer console in the browser, I see
Failed to load resource: the server responded with a status of 500 ()
(don't actually know how to interpret this) and looking at the usage stats in the firebase dashboard, can see that the function is being activated (just not working smoothly). The exact code being used to deploy looks like
//import * as functions from 'firebase-functions';
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest((req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
return admin.firestore().ref('/messages').push({original: original})
.then((snapshot) => {
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
return res.redirect(303, snapshot.ref.toString());
});
});
Never used google cloud functions before and any debugging info or solutions of what could be going wrong here would be appreciated.
Ultimately, would like to end up using cloud functions to work with the firestoreDB, while the official tutorial appears to be intended for use with the firebaseDB. So if there are any particular changes that would need to be made in that regard, advice in that would be appreciated as well.
Upvotes: 1
Views: 789
Reputation: 83163
The following line in your code is wrong:
return admin.firestore().ref('/messages').push({original: original}).then()
You are "mixing up" the Real Time Database and Firestore which should be queried (i.e. write, read, delete) differently, since their data-model are different, see https://firebase.google.com/docs/firestore/rtdb-vs-firestore. In particular, while "both Realtime Database and Cloud Firestore are NoSQL databases", the first one "stores data as one large JSON tree" while the latter "stores data in documents organized in collections".
As a matter of facts, the original code from the "Get Started" help item you refer to is targeting the Real Time Database with admin.database()
return admin.database().ref('/messages').push({original: original}).then()
Replacing, in this line of code, database()
by firestore()
will not work.
You should have a look at the Firestore documentation, here, here and here for example, to learn how to write to Firestore.
For example, you could modify your Cloud Function as follows:
exports.addMessage = functions.https.onRequest((req, res) => {
// Grab the text parameter.
const original = req.query.text;
admin.firestore().collection("mydocs").doc("firstdoc").set({original: original})
.then(() => {
console.log("Document successfully written!");
res.send({original: original}); //Just an example, as there is not that much interest to send back the value of original...
})
.catch(error => {
console.error("Error writing document: ", error);
res.status(500).send(error);
});
})
Finally I would suggest you watch the following official Video Series "Learning Cloud Functions for Firebase" (here), and in particular the three videos titled "Learn JavaScript Promises". You will note, in particular, that for HTTP triggered functions you should just send the response, without using return
.
Upvotes: 2