简体   繁体   中英

Triggering Google Cloud function using console

I am trying to use the google cloud console to test a cloud function. Below is a snippet.

exports.requestCreated = functions.firestore
    .document('users/{userId}/requests/{requestId}')
    .onWrite((change, context) => {
    // execute operation
    });

I have tried all sorts of combination of JSON data. Eg

{"userId":"Xl86pqOpF9T2MAn12p24OJAfYJW2","requestId":"abc1234"}

But I keep getting the following statement in logs:

Request created by {userId} 

The actual userId is not being read from the JSON data in the console. Can you help?

This is not a problem with the execution of the cloud function. It's a problem with hardcoding the string.

'users/{userId}/requests/{requestId}' is a hardcoded string. Node.js will not automatically replace {userId} with the value of the variable userId .

Following this previous SO post , try something like this using template strings :

`users/${userId}/requests/${requestId}`

Please note it is surrounded by backticks (`), not single quotes (').

This assumes you already have a userId and requestId variables defined. You must restructure your cloud function like this to retrieve that data. Notice that the specific variable values must be extracted from the event variable.

Thank you, Nareddyt. The function is for Firestore, and the way it is written right now checks if a new document is created under the collection requests. I tried replacing the string as you suggested, but as you pointed out, it requires these variables to be defined. I do not quite understand how to restructure the cloud function because the syntax I have used is how event detection is suggested in the Firestore documentation. My function currently works in its entirety, but testing it is a major pain. I have to go through my mobile app and do the whole userflow to test this function. I am new to Node.js and any guidance would be appreciated.

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