[英]Firebase function returning null in Flutter despite running successfully
I've seen very similar questions but I still can't see what's wrong with my code.我见过非常相似的问题,但我仍然看不出我的代码有什么问题。
I've deployed a Firebase function that I call from my client when pressing a specific button.我已经部署了一个 Firebase function,我在按下特定按钮时从我的客户那里调用它。
// Generate Payment URL
exports.paymentRequest = functions.https.onCall(async (data, context) => {
const clientAccessToken = data.clientAccessToken;
const recipientIban = data.recipientIban;
const recipientName = data.recipientName;
const paymentDescription = data.paymentDescription;
const paymentReference = data.paymentReference;
const productPrice = parseInt(data.productPrice);
const jsonData = {
"destinations": [
{
"accountNumber": recipientIban,
"type": "iban"
}
],
"amount": productPrice,
"currency": "EUR",
"market": "FR",
"recipientName": recipientName,
"sourceMessage": paymentDescription,
"remittanceInformation": {
"type": "UNSTRUCTURED",
"value": paymentReference
},
"paymentScheme": "SEPA_INSTANT_CREDIT_TRANSFER"
};
axios({
method: "post",
url: "https://api.endpoint.com/payment",
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${clientAccessToken}`,},
data: jsonData,
})
.then(response => {
//handle success
console.log(response.data);
return response.data;
})
.catch(response => {
//handle error
console.log(response);
});
})
And here is my flutter code:这是我的 flutter 代码:
onPressed: () async {
HttpsCallable callable = FirebaseFunctions.instance
.httpsCallable('PaymentRequest');
final resp = await callable.call(
<String, dynamic>{
'clientAccessToken': 'myToken',
'recipientIban': ibanController.text,
'recipientName': nameController.text,
'paymentDescription': descriptionController.text,
'paymentReference': referenceController.text,
'productPrice': productPriceController.text
},
);
print("result: ${resp.data}");
}
I do receive a 200 and the full expected API Response body in the Firebase Console (meaning that the console.log(response.data); in my function works), but I always receive a "result: null" in de Flutter console, so it seems like the response.data isn't returned. I do receive a 200 and the full expected API Response body in the Firebase Console (meaning that the console.log(response.data); in my function works), but I always receive a "result: null" in de Flutter console,所以看起来 response.data 没有返回。
I tried adding a return
right before axios
in my function but it doesn't change anything.我尝试在我的axios
中的 axios 之前添加一个return
,但它并没有改变任何东西。
What am I missing?我错过了什么?
Thanks谢谢
You returning data in axios.then()
method.您在axios.then()
方法中返回数据。 If you made an async function why you not use await
but .then()
?如果您制作了异步 function 为什么不使用await
而使用 .then .then()
?
Try like this:试试这样:
exports.paymentRequest = functions.https.onCall(async (data, context) => {
const clientAccessToken = data.clientAccessToken;
const recipientIban = data.recipientIban;
const recipientName = data.recipientName;
const paymentDescription = data.paymentDescription;
const paymentReference = data.paymentReference;
const productPrice = parseInt(data.productPrice);
const jsonData = {
"destinations": [
{
"accountNumber": recipientIban,
"type": "iban"
}
],
"amount": productPrice,
"currency": "EUR",
"market": "FR",
"recipientName": recipientName,
"sourceMessage": paymentDescription,
"remittanceInformation": {
"type": "UNSTRUCTURED",
"value": paymentReference
},
"paymentScheme": "SEPA_INSTANT_CREDIT_TRANSFER"
};
const response = await axios({
method: "post",
url: "https://api.endpoint.com/payment",
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${clientAccessToken}`,
},
data: jsonData,
})
console.log(response.data)
return response.data
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.