TypeError:res.send(...).then不是一个函数 [英] TypeError: res.send(...).then is not a function
问题描述
在我的 index.js
内,我有一个函数的结尾如下:
Inside of my index.js
I have a function which ends like this:
return res.send(response).then(saveProductID(uid, product.id));
在Firebase控制台中,它说:
In the Firebase console its says:
TypeError:res.send(...).那么它不是一个函数
TypeError: res.send(...).then is not a function
完整代码示例:
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
会弹出此错误?
.then()
has worked before (and continues to) for many other functions. So why is this error popping up for the createConnectAccount
?
推荐答案
我在文档中或源暗示Response.send将返回Promise,也看不到任何假定这样做的代码示例.它不是一个异步函数,在常见的成功情况下,它似乎会返回 this
(甚至没有记录).
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).
我想知道您是否一直在变得幸运"?因为您一直在使用 .then()
而不是在 res.send
的特定返回值上使用,而是在返回的异步函数的返回值上使用了此功能它:
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.'));
由于JS运行时会自动将 async
函数的返回值包装在Promise中,因此在使用此模式时,您将始终获得一个可以转换的对象.
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);
这篇关于TypeError:res.send(...).then不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!