简体   繁体   中英

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

I'm using React native with Firebase. I deployed a function (without query params) and I'm able to call it without problems from my iPhone. When I add parameters, I'm able to run it only on my browser, but on my phone the parameters are undefined.

Firebase function

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

Client App

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

I'm suspecting that this is a bug in either Firebase SDK, React Native's translation to iOS or hopefully a problem in my code, what could be the problem?

To call firebase functions from app, do not use onRequest ; use onCall . See the doc for detailed implementation on both server and client side.

This is also mentioned in the doc of react-native-firebase , but the use of onCall is not emphasized (I think it should be highlighted to avoid confusion).

That said, it is possible to call an onRequest function from app, but the parameters are not under request.query , but under request.body.data . For example, in OP's scenario, the firebase function would be

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

However, calling onRequest from app is not recommended, because the success or error message sent back to the client cannot be properly handled by react-native-firebase . Thus, the best practice is to use onCall .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM