Hello, I was wondering if it was possible to do query parameters with CloudCode JS? I’ve been trying to use this.request.queryParams and this.request.pathParams but it seems that both don’t work when using the query notation for REST APIs? I also can’t use @route GET /groups as that will conflict with another route within my API. What would be the approach to solving this?
is entirely possible and I am able to access the parameters through this.request.pathParams.{parameter}. However, I would like to follow the query parameter notation as stated in the beginning of this post.
Also, would like to notice that await and then/catch after return are optional, as the server will wait for the result and return it, or throw an error if it happens.
As for:
I also can’t use @route GET /groups as that will conflict with another route within my API.
You need to rename one of the route, as you cannot have two different routes with the same name.
Would there be any way to have the same route @route GET /groups and support query parameters? Or is the idea to first put the parameters in the API and then to handle each case? Sorry, very new to this!
Would there be any way to have the same route @route GET /groups and support query parameters?
This is simply not technically possible, since the query parameters do not identify the route. From the point of view of the system, it will be the same route that can take any parameters.
I would recommend making only one method and based on the presence of parameters do different logic - if they come, one function is called, if not, another.
Something like:
/**
* @route GET /groups
* @param {String} groupName
* @param {String} groupDomain
* @returns {Promise<Array>}
*/
async getGroups(groupName, groupDomain) {
const where = (groupName && groupDomain) ? `name LIKE '${groupName}%' and domain='${groupDomain}'` : ''
return Backendless.Data.of('groups').find({ where })
}