Async/await: Unexpected token client

I am writing an app for Freshdesk that contains a serverside component for displaying additional information about the user that posted a ticket. This additional information is retrieved from an external resource through HTTPs using the request method with request templates.

In my development environment, I am using fdk 9.0.8, npm 10.4.0 and node v18.17.1 on macOS.

When I run fdk run on the command line, the following error is displayed:

Validation failed due to the following issue(s):
✖ app/app.js::64: Parsing error: Unexpected token client

On line 64 of app.js, the serverside function is invoked:

let result = await client.request.invoke("onTicketCreateHandler", { data });

This is the relevant code snippet from app.js:

function getTicketDetails(data, error) {
	client.data.get("ticket")
		.then(data)
		.catch(error);
}

async function updateUserInformation(client) {
    getTicketDetails(
        function (data) {
            try {
                let result = await client.request.invoke("onTicketCreateHandler", { data });
            }
            catch (error) {
                console.error(error);
            }
        },
        function (error) {
            console.error(error);
        });
}

I cannot use client.request.invokeTemplate() in app.js because the username and password for the HTTP request are defined as secure values in iparams.json and are therefore not accessible in app.js.

The relevant code from server.js is:

onTicketCreateHandler: async function(args) {
    await $request.invokeTemplate("getUserInformation", {
        context: {
            "email": args.data.ticket.sender_email
        }
    });
}

The request is defined in requests.js as follows:

{
    "getUserInformation": {
        "schema": {
            "protocol": "https",
            "method": "GET",
            "host": "somewhere.com",
            "path": "/path/to/the/api",
            "headers": {
                "Authorization": "Basic <%= encode(iparam.api_username + ':' + iparam.api_password) %>",
                "Content-Type": "application/json"
            },
            "query": {
                "email": "<%= context.email %>",
                "token": "<%= iparam.api_security_token %>"
            }
        },
        "options": {
            "retryDelay": 1000,
            "isOAuth": false
        }
    }
}

Both the request and the serverside function are registered in manifest.js:

{
    "platform-version": "2.3",
    "product": {
        "freshdesk": {
            "location": {
                "ticket_sidebar": {
                    "url": "template.html",
                    "icon": "./resources/img/icon.svg"
                }
            },
            "functions": {
                "onTicketCreateHandler": {
                    "timeout": 15
                }
            },
            "requests": {
                "getUserInformation": {}
            }
        }
    },
    "engines": {
        "node": "18.17.1",
        "fdk": "9.0.8"
    }
}

When I change the async function and await client.request.invoke in app.js to a synchronous function, I do not get the error message when I run fdk run, but the serverless function is not invoked properly.

Can anyone please point out what I am doing wrong or what I am missing?

Hi @virtualsmarthome

Could you try the below changes and see if they help?

Also, I would recommend you change your invoke function name from onTicketCreateHandler to some other so that there is no confusion is you are triggering serverless event or invoke event

async function updateUserInformation(client) {
  try {
    let ticketDetails = await client.data.get("ticket");
    let result = await client.request.invoke("invokeFunction", { ticket: ticketDetails });
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}

Dear Mohammed,

Thanks for your fast response. Woohoo! It works :grinning:

Just to clarify what I have been doing wrong. Does this mean, I can only use either (a) async/await or (b) .then() but not both at the same time in one function? Or what else have I exactly been doing wrong.

Thanks again for your help and best regards,
Josef

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.