polar
polar

Reputation: 522

Firebase functions query parameters are null

I am having difficulty passing query parameters to my firebase (google cloud) functions, they are continuously empty, but otherwise the function completes as normal. Can anyone identify whats going wrong?

Cloud Function Code

exports.message = functions.https.onRequest((request, response) => {
   functions.logger.log("log query");
   functions.logger.log(request.query);
  response.json({result: `done`});
});

Local application code:

export const sendPushNotification = async (options) => {
  try {
    const db = firebase.firestore();
    const fbFunctions = firebase.functions();

    const message = fbFunctions.httpsCallable("message");
   message({text:"asdfasdf"})
  
  } catch (error) {
    console.log({ error });
    switch (error.code) {
      default:
        return {
          error: "Error.",
        };
    }
  }
};

Upvotes: 0

Views: 674

Answers (1)

Priyashree Bhadra
Priyashree Bhadra

Reputation: 3597

It's not possible to use the Firebase Functions SDK to invoke onRequest type functions. The Firebase SDK implements the client side of a callable function that you declare with onCall. You're using onRequest here, which means you're writing a standard HTTP type function. For this type of function, you should use a standard HTTP client (not the Firebase SDK). If you actually did want to use the Firebase SDK to invoke your function, you will have to write a callable function instead. Note that callable functions have their own spec, and you won't be able to easily invoke them, for e.g. from Postman.

Use functions.https.onCall to create an HTTPS callable function. This method takes two parameters: data, and optional context: Here is the code that should work for you :

exports.addMessage = functions.https.onCall((data, context) => {
const text = data.text;
if (!(typeof text === 'string') || text.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
        'one arguments "text" containing the message text to add.');
}

if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
        'while authenticated.');
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;

const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
    text: sanitizedMessage,
    author: { uid, name, picture, email },
}).then(() => {
  console.log('New Message written');

return { text: sanitizedMessage };
})

.catch((error) => {
throw new functions.https.HttpsError('unknown', error.message, error);
});

});

Set up the client development environment with the following steps below:

  1. Add Firebase to your Web App.
  2. Add the Firebase core and Cloud Functions client libraries to your app:

enter image description here 3. Run npm install [email protected] --save
4. Manually require both Firebase core and Cloud Functions:

const firebase = require("firebase");
// Required for side-effects
require("firebase/functions");
  1. Initialise the Client SDK using :

enter image description here

  1. Call the function using: enter image description here

Upvotes: 1

Related Questions