简体   繁体   中英

Firestore Cloud Function - How to read params from url

I need to extract the params from http url in firestore cloud function,
HTTP URL

 https://us-central-xxxxxxxxxxx.cloudfunctions.net/somefunction?name=ABC&age=80

Firestore Cloud function

export const somefunction = functions.https.onRequest(async (request, response) => {
  var name = // need to extract name from request
  var age = // similarly need to extract age from request
}

Can anyone help me out with this?

You can use the request object to access the query parameters similar to what you do in Express Js

export const somefunction = functions.https.onRequest(async (request, response) => {
  var name = request.query.name
  var age = request.query.age
}

If the request was from a POST request then you can use request.body.* for accessing values.

I have a similar issue where I cannot get req.url and req.query.abc as asked in Cannot get req.path and req.query.abc with firebase functions

As mentioned in the comments of Call Firebase cloud function via GET and return parameters this is happening only on development.

After searching for a long time I came across this article https://howtofirebase.com/cloud-functions-migrating-to-node-8-9640731a8acc where it said firebase-tools have to be of 4.0.0 version. I've updated my firebase-tools to 4.0.0 and I can get the query parameters normally

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