简体   繁体   English

TypeError: res.send(...).then 不是 function

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

Inside of my index.js I have a function which ends like this:在我的index.js里面,我有一个 function ,它的结尾是这样的:

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

In the Firebase console its says:在 Firebase 控制台中,它说:

TypeError: res.send(...).then is not a function TypeError: res.send(...).then 不是 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. .then()以前(并继续)用于许多其他功能。 So why is this error popping up for the createConnectAccount ?那么为什么createConnectAccount会弹出这个错误呢?

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.我在文档或源代码中看不到任何暗示 Response.send 将返回 Promise 的内容,我也没有看到任何假设它的代码示例。 It is not an async function, and it seems it will return this in the common success case (and even this is not documented).它不是异步 function,它似乎会在常见的成功案例中返回this (甚至没有记录)。

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:我想知道您过去是否对此感到“幸运”,因为您一直在使用.then()不是在res.send的特定返回值上,而是在返回它的异步 function 的返回值上使用:

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.因为 JS 运行时会自动将async function 的返回值包装在 Promise 中,所以在使用此模式时,您总是会得到一个可用的 object。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM