Yasin G.
Yasin G.

Reputation: 43

Unhandled error TypeError: handler is not a function Firebase functions

I am trying to add poke notifications on my app. And i am stuck to send notifications with firebase functions and message.I deployed my functions to firebase with firebase deploy --only functions. And i am getting error to server side. I am getting this error on my functions :

Unhandled error TypeError: handler is not a function
    at fixedLen (/workspace/node_modules/firebase-functions/lib/v2/providers/https.js:153:37)
    at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:544:32
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)**

And these are my functions:

const functions = require("firebase-functions");
const admin = require("firebase-admin"); //admin sdk

 exports.sendPoke= functions.https.onCall(async (data, context) => {
  const sentBy = context.auth?.uid; // sender
  const sentTo = data.sentTo;       // reciever
  const message = data.message || "Seni dürttü!";
  const currentTime = admin.firestore.Timestamp.now();

  if(!sentBy){
    console.error("Failed to add Sender User");
  }
  if(!sentTo){
    console.error("Submitted User could not be added");
  }
  
  if (!sentBy || !sentTo) {
    throw new functions.https.HttpsError(
      "invalid-argument",
      "Sender and recipient information is required."
      
    );
  }

  try {

    // Bildirimi gönder
    const payload = {
      notification: {
        title: "A new poke!",
        body: `poked you!`,
      },
      data: {
        sentBy,
        sentTo,
        message,
      },
    };
//I am suspecting this line
    await admin.messaging().sendToDevice(sentTo, payload);//send noticfication only one user

    return { success: true, message: "Poke sent." };
  } catch (error) {
    console.error("Poke sent error:", error);
    throw new functions.https.HttpsError("unknown", "Bir hata oluştu.");
    
  }
});

and this my index.js file context :

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


// if app already started dont start again
if (!admin.apps.length) {
    admin.initializeApp();
  }

// export sendPoke function
const sendPoke = require("./functions/utilities/pokeAndNotifications/sendPoke");

exports.sendPoke = functions.https.onCall(sendPoke);

And this is how i use this firebase function on my flutter app :

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:cloud_functions/cloud_functions.dart';

Future<void> sendPokeNotification(String sentTo) async {
  try {
    // call callable function
     final HttpsCallable callable =
        FirebaseFunctions
        .instance
        .httpsCallable('sendPoke');
        
    // send data and give respond
    final response = await callable.call(<String, dynamic>{
      "sentTo": sentTo, // reciever UID
      "message": 'Hi, i poked you!', // message content
    });

    print('Function respond: ${response.data}');
  } catch (e) {
    print('Send Poke Error: $e');
  }
}

Also i tried this ways to resolve my problem :

on index.js file

const sendPoke = require("./functions/utilities/pokeAndNotifications/sendPoke");

exports.sendPoke = functions.https.onCall(sendPoke); 

on sendPoke file

module.exports.sendPoke= functions.https.onCall(async (data, context) => {
  const sentBy = context.auth?.uid; // sender
  const sentTo = data.sentTo;       // reciever
  const message = data.message || "Seni dürttü!";
  const currentTime = admin.firestore.Timestamp.now();

  if(!sentBy){
    console.error("Failed to add Sender User");
  }
  if(!sentTo){
    console.error("Submitted User could not be added");
  }

index.js and sendPoke.js

exports.sendPokeYourFriend= functions.https.onCall(async (data, context) => {
  const sentBy = context.auth?.uid; // sender
  const sentTo = data.sentTo;       // reciever
  const message = data.message || "Seni dürttü!";
  const currentTime = admin.firestore.Timestamp.now();

  if(!sentBy){
    console.error("Failed to add Sender User");
  }
  if(!sentTo){
    console.error("Submitted User could not be added");
  } 

exports.sendPoke = functions.https.onCall(sendPoke.sendPokeYourFriend);

Upvotes: 0

Views: 86

Answers (1)

Manolis Dame
Manolis Dame

Reputation: 53

The issue is with

await admin.messaging().sendToDevice(sentTo, payload);

Yes but it is because you haven't initialised admin, only imported it.

Add admin.initializeApp() at the top and you should be fine. Fingers Crossed!

Upvotes: 0

Related Questions