26

How can I set some parameters on my HTTPS functions in Firebase? I am building an app, and while building the app, I have managed to grow my mailing list. Now I want to send mails out, but I want to make sure that they can unsubscribe before I send anything out.

I am using Firebase for everything, and I have managed to make a function that sends mails out to every subscribed mail.

I am also able to "unsubscribe" a specific mail, but that is hardcoded, and not at all an optimal solution.

exports.testUnsub = functions.https.onRequest((req, res) => {
  var db = admin.database();
  var ref = db.ref("mailingList/-KhBOisltrOmv57Mrzus");
  ref.child("subscribed").set(false);
  console.log("-KhBOisltrOmv57Mrzus has unsubscribed from mailing list.");
});

In the mail I send there is an URL, which triggers this HTTPS function. I want to set a parameter on that URL so it becomes dynamic. Something like:

https://us-central1-<project-id>.cloudfunctions.net/testUnsub?listID=xxxxxxxxxxx

I am looking for anything that can get me on the right direction.

1 Answer 1

49

It's important to know that the req and res parameters to your https function are Express.js Request and Response objects.

The Request object contains all the data about the request coming from the client, including the query that the client sent in the URL. It will take the form req.query.name_of_the_parameter.

Sign up to request clarification or add additional context in comments.

5 Comments

But i get "Your client does not have permission to get URL" when i use req.query.titl
Doug is there any way with Cloud Functions to define and extract route parameters, similar to Express where you would do something like app.get('/users/:userId/books/:bookId', function (req, res) { ... }?
@PatNeedham you can check this github link or firebase doc which utilizes the firebase functions as a normal express router. So that you can match with the endpoints in the ways you prefer.
For those that don't understand http request format, do https://us-central1-<project-id>.cloudfunctions.net/testUnsub?exampleParam=x&anotherExampleParam=y, and then use the req.query.exampleParam to access the contents.
@JoeMoore The question from Pat is about route parameters like /users/123/ whereas your answer is about /?user=123. Different types of parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.