Query Parameters in CloudCode JS

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?

/**
     * @route GET /groups?groupName={groupName}&groupDomain={groupDomain}
     * @returns {Promise<Array>}
     */
    async getGroupsBySearch() {
        const groupName = this.request.queryParams.groupName;
        const groupDomain = this.request.queryParams.groupDomain;
        console.log("groupName:" + groupName + ", groupDomain:" + groupDomain); 
        let whereClause = `name LIKE '${groupName}%' and domain='${groupDomain}'`;
        let queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause(whereClause);
        return await Backendless.Data.of("groups").find(queryBuilder)
            .then(function (res) {
                return res;
            })
            .catch(function (error) {
                return error;
            });
    }

I should also note that using a @route such as:

@route GET /groups/{groupName}/{groupDomain}

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.

Hi, @Social_Smarttwigs

In your case the method will look like:

/**
 * @route GET /groups
 * @param {String} groupName
 * @param {String} groupDomain
 * @returns {Promise<Array>}
 */
async getGroupsBySearch(groupName, groupDomain) {
  console.log('groupName:' + groupName + ', groupDomain:' + groupDomain)
  const whereClause = `name LIKE '${groupName}%' and domain='${groupDomain}'`

  return Backendless.Data.of('groups').find({ where: whereClause })
}

Please note this doc:

Type Annotations - Developing Backendless Server Code with node.js

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.

Regards,
Marina

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!

Hi @Social_Smarttwigs,

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 })
}

Please let me know if you have any questions.

Regards,
Stanislaw

Oh, that makes a lot more sense! Thanks for the help!