JS help - Code returning "undefined" instead of object

Describe the issue/error/question

Hello, I hope this is an appropriate question to ask in this forum. If not, please let me know.

I am trying to use the Code node, in conjunction with a form submission webhook node, plus the Google Workspace Admin node. This Code node is meant to check against the Google Workspace API, to see if a username is available. It goes through iterations of possible usernames, until it finds an available, valid one.

I really do not know enough about JavaScript to debug this. But I was hoping this community might be able to assist. I’m sure I’m missing something simple - like incorrectly mapping the JSON result from the previous (webhook) node.

Code snippet

async function main(node) {
  // First, let's define the variables we need to create a user
  const { firstName, lastName } = node.Tripetto.json.body;

  // Now let's define the main function that will check for available usernames
  async function findAvailableUsername() {
    let username = firstName[0] + lastName; // Try first initial + last name
    let searchResults = await node.GoogleWorkspace.searchUser({
      query: username,
      viewType: 'domain_public'
    });
    if (searchResults.length === 0) {
      // If the username is available, return it
      return { username };
    } else {
      // If the first option is not available, try first name + last name
      username = firstName + lastName;
      searchResults = await node.GoogleWorkspace.searchUser({
        query: username,
        viewType: 'domain_public'
      });
      if (searchResults.length === 0) {
        return { username };
      } else {
        // If the second option is not available, try adding a number to the end of the last name
        let i = 1;
        while (true) {
          username = firstName + lastName + i;
          searchResults = await node.GoogleWorkspace.searchUser({
            query: username,
            viewType: 'domain_public'
          });
          if (searchResults.length === 0) {
            return { username };
          }
          i++;
        }
      }
    }
  }

  // Finally, let's call the main function and return the result
  return findAvailableUsername();
}

What is the error message (if any)?

Code doesn’t return an object (instead returning “undefined”)

Please share the workflow

Share the output returned by the last node

[
  {
    "headers": {
      "host": "n8n.example.tld",
      "content-length": "227",
      "accept": "application/json",
      "accept-encoding": "gzip, deflate",
      "content-type": "application/json",
      "x-forwarded-for": "127.0.0.1",
      "x-forwarded-host": "n8n.example.tld",
      "x-forwarded-proto": "https"
    },
    "params": {},
    "query": {},
    "body": {
      "site": "string",
      "firstName": "text",
      "lastName": "text",
      "tripettoId": "00000000000000000000",
      "tripettoCreateDate": "2023-01-01T20:11:14.131Z",
      "tripettoFingerprint": "<fingerprint>"
    }
  }
]

Information on your n8n setup

  • n8n version: 0.209.4
  • Database you’re using (default: SQLite): SQLite
  • Running n8n with the execution process [own(default), main]: Default
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

Thank you!

Welcome to the community @tmeuze !

Not at a computer right now but you do not return anything currently. You never call that main function. You would need at least something like

return main($input.first().json);

at the end.

1 Like

Hello - thank you for your reply, and happy New Year!

In addition to what you said, I also realize that the method searchUser does not exist. I’m not quite sure how to determine what methods are available for this node.

To be fully transparent - I am aided by ChatGPT, which is helping me to build this functionality. I do hope that does not violate Community Rules.

Thank you, again.

PS - incredible software, n8n is.

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