【问题标题】:Firebase function returning null in Flutter despite running successfully尽管运行成功,Firebase 函数在 Flutter 中返回 null
【发布时间】:2022-01-17 18:42:16
【问题描述】:

我看到了非常相似的问题,但我仍然看不出我的代码有什么问题。

我部署了一个 Firebase 函数,当按下特定按钮时我会从客户端调用该函数。

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

这是我的颤振代码:

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

我确实在 Firebase 控制台中收到了 200 和完整的预期 API 响应正文(这意味着我的函数中的 console.log(response.data); 有效),但我总是在 de 中收到“结果:null” Flutter 控制台,所以好像没有返回 response.data。

我尝试在我的函数中的axios 之前添加一个return,但它并没有改变任何东西。

我错过了什么?

谢谢

【问题讨论】:

    标签: firebase flutter axios


    【解决方案1】:

    您在axios.then() 方法中返回数据。如果您创建了异步函数,为什么不使用 await 而使用 .then()

    试试这样:

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

    【讨论】:

    • 感谢您的回复。我已经按照建议更改了我的函数,但现在颤动会引发​​错误: FirebaseFunctionsException ([firebase_functions/internal] INTERNAL At this line " final resp = await callable.call( "
    • @GlizzyGlider 我不能买任何东西来感谢。 XD 如果我的回答是正确的,请投票并在投票箭头下方标记“鸟”。
    • 我已经编辑了我的答案,我在尝试格式化 lol 时按了 enter 而不是 shift enter。您的解决方案出现了一个新问题>_
    • @GlizzyGlider 我没有颤抖。也许resp.data() 是一个函数而不是一个变量?当你写resp. 按下 ctrl + 空格你会看到所有的 evable 命令。
    • 好的,所以我实际上发现并解决了这个问题。因此,即使 paymentRequest 是一个异步函数,我仍然需要将 axios 请求包装在另一个异步函数中,否则会出现错误。但是,我仍然在客户端得到空结果响应。 :-(
    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 2021-09-28
    • 2013-03-10
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多