【问题标题】:TypeError: res.send(...).then is not a functionTypeError: res.send(...).then 不是函数
【发布时间】:2021-07-24 18:33:57
【问题描述】:

在我的index.js 内部,我有一个这样结束的函数:

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

在 Firebase 控制台中显示:

TypeError: res.send(...).then 不是函数

完整代码示例:

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() 以前(并将继续)用于许多其他功能。 那么为什么createConnectAccount 会出现这个错误?

【问题讨论】:

    标签: javascript ios firebase google-cloud-functions


    【解决方案1】:

    我在文档或source 中没有看到任何暗示 Response.send 将返回 Promise 的内容,我也没有看到任何假设它会返回的代码示例。它不是一个异步函数,它似乎会在常见的成功案例中返回this(甚至没有记录)。

    我想知道您过去是否对此感到“幸运”,因为您一直在使用 .then() 而不是 res.send 的特定返回值,而是返回它的异步函数的返回值:

    async foo(res) {
      return res.send();
    }
    
    foo().then(a => console.log('this will work.'));
    

    因为 JS 运行时会自动将 async 函数的返回值包装在 Promise 中,所以在使用此模式时,您总是会得到一个 thenable 对象。

    我不确定您的代码 sn-p 中的细节,但我相信以下内容会出现您似乎想要的行为:

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

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2020-10-20
      • 2017-06-24
      • 2023-03-28
      • 2021-05-15
      • 1970-01-01
      相关资源
      最近更新 更多