-1

I'm new to server development and trying to get my cloud firebase function working. I'm getting a res.send() is not a function on my firebase Log when my stripe webhook fires and I'm not too sure why. I'm pretty sure I'm missing something. The only conclusion I can come up with is that it is because I'm not using App from const app = express(); but I'm seeing it used elsewhere.

Any and all help/direction is appreciated

I'm getting the following:

res.send() is not a function

res.end() is not a function

res.json() is not a function

Anything that is dealing with res seems to be an issue.

Here is my code:

const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
const Chatkit = require('@pusher/chatkit-server');
const stripeToken = require('stripe')(functions.config().stripe.token);
const stripeWebhooks = require('stripe')(functions.config().keys.webhooks);
const express = require('express');
const cors = require('cors');
const endpointSecret = functions.config().keys.signing;
const request = require('request-promise');
const app = express();

app.use(cors({ origin: true }));

exports.stripeCreateOathResponseToken = functions.https.onRequest(cors((req, res) => {

    const endpointSecret = "whsec_XXXXXXXXXXXXXXXXXXX";

    // Get the signature from the request header
    let sig = req.headers["stripe-signature"];

    let rawbody = req.rawBody;
    // res.send("testing res.send()"); // doesnt work. cant use res.send() here

    console.log("rawbody: " + rawbody);
    console.log("request.body: " + req.body);
    console.log("request.query.code: " + req.query.code);
    console.log("request.query.body: " + req.query.body);
    console.log("request.query.state: " + req.query.state);
    // console.log("res.body: " + res.json({received: true}));
    stripeWebhooks.webhooks.constructEvent(req.rawBody, sig, endpointSecret);
    res.end("testing res.end()"); // doesnt work. cant use res.end() here
}));



Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Cflux
  • 1,423
  • 3
  • 19
  • 39

1 Answers1

2

That doesn't look like the correct way to use the cors module with Cloud Functions for Firebase. Try this instead:

exports.stripeCreateOathResponseToken =
functions.https.onRequest((req, res) => {
    return cors(req, res, () => {
        // put your function code here
    });
});

Cribbed from an official sample.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441