Awsome_user
Awsome_user

Reputation: 65

TypeError: res.send(...).then is not a function

Inside of my index.js I have a function which ends like this:

return res.send(response).then(saveProductID(uid, product.id));

In the Firebase console its says:

TypeError: res.send(...).then is not a function

Full code example:

exports.createConnectAccount = functions.https.onRequest(async (req, res) => {
    var data = req.body
    console.log(data, "<--  clark jable")
    var uid = data.userID
    console.log(uid, "<--  this is the uid")
    var email = data.email
    var response = {}
    strip.accounts.create({
            type: 'express',
            country: 'US',
            requested_capabilities: [
                'transfers',
            ],
            business_type: 'individual',
        },
        (err, account) => {
            if (err) {
                console.log("Couldn't create stripe account: " + err)

                return res.send(err)
            }
            // createStripe_customers(uid, customer, intent)
            console.log("ACCOUNT: " + account.id)
            response.body = {
                success: account.id
            }
            //createStripe_customers()
            return res.send(response).then(createStripe_Accounts(uid, account));
        }
    );
});

function createStripe_Accounts(uid, account) {
    console.log(uid, " did the createStripe_Accounts Run? ", account.id)
    const userRef = admin.database().ref('Stripe_Accounts').child(uid) //.child(uid)
    return userRef.set({
        account_id: account.id,
    });
}

.then() has worked before (and continues to) for many other functions. So why is this error popping up for the createConnectAccount ?

Upvotes: 1

Views: 522

Answers (1)

Myk Willis
Myk Willis

Reputation: 12879

I see nothing in the documentation or source that implies Response.send will return a Promise, nor do I see any code examples that assume it does. It is not an async function, and it seems it will return this in the common success case (and even this is not documented).

I wonder if you have been "getting lucky" with this in the past because you have been using .then() not on the specific return value of res.send but instead on the return value of an async function that returns it:

async foo(res) {
  return res.send();
}

foo().then(a => console.log('this will work.'));

Because the JS runtime will automatically wrap the return value of an async function in a Promise, you'll always get a thenable object when using this pattern.

I'm not sure about the specifics in your code snippet, but I believe the following will have the behavior you seem to be after:

res.send(response)
return createStripe_Accounts(uid, account);

Upvotes: 2

Related Questions