簡體   English   中英

Firebase (with react native) HTTPS function 沒有收到參數

[英]Firebase (with react native) HTTPS function doesn't receive parameters

我將 React native 與 Firebase 一起使用。我部署了一個 function(沒有查詢參數),我可以毫無問題地從我的 iPhone 調用它。 當我添加參數時,我只能在我的瀏覽器上運行它,但在我的手機上參數是未定義的。

Firebase function

exports.testFunction = functions.https.onRequest((request, response) => { 
    const searchQuery = request.query.search;
    response.status(200).send({data:searchQuery});    
});

客戶端應用

const testFunction = functions.httpsCallable('testFunction');
testFunction({search: "anything"})

我懷疑這是 Firebase SDK 中的錯誤,React Native 對 iOS 的翻譯,或者希望是我的代碼中的問題,可能是什么問題?

要從應用程序調用 firebase 函數,請不要使用onRequest 使用onCall 有關服務器端和客戶端的詳細實現,請參閱文檔

這個在react-native-firebase firebase的文檔中也有提到,但是並沒有強調onCall的使用(我覺得還是應該強調一下,以免混淆)。

也就是說,可以從應用程序調用onRequest function,但參數不在request.query下,而是在request.body.data下。 例如,在 OP 的場景中, firebase function 將是

exports.testFunction = functions.https.onRequest((request, response) => { 
  const searchQuery = request.body.data.search; // param is not in query
  response.status(200).send({data:searchQuery});    
});

但是,不建議從應用程序調用onRequest ,因為返回給客戶端的成功或錯誤消息無法由react-native-firebase正確處理。 因此,最佳做法是使用onCall

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM