Code piece issue - TypeError: e.code is not a function

Hi all, I am trying to write a simple function to search for user’s LDAP.

the code is as follows:

import LDAP from 'ldapjs-client';

interface LdapConfig {
  url: string;
  bindUsername: string;
  bindDN: string;
  bindCredentials: string;
  searchBase: string;
  searchFilter: string;
  reconnect: boolean;
}

const ldapConfig: LdapConfig = {
  url: '...,
  bindDN: 'uid=...,o=...,o=...',
  bindCredentials: '...',
  searchBase: 'o=...,o=...',
  searchFilter: '(uid={{...}})',
  reconnect: true
};

const client = new LDAP({ url: ldapConfig.url });

async function bindClient(): Promise<void> {
  try {
    console.log("Start binding...")
    await client.bind(ldapConfig.bindDN, ldapConfig.bindCredentials);     console.log("Binding successful.");
  } catch (e) {
    console.error("Binding failed.", e);
  }
}

interface SearchOptions {
  filter: string;
  scope: 'base' | 'one' | 'sub';
  attributes: string[];
}

async function searchUser(searchUid: string): Promise<any[]> {   try {
    console.log("Start searching for user...")
    const searchOptions: SearchOptions = {
      filter: `(uid=${searchUid})`,
      scope: 'sub',
      attributes: ['dn', 'sn', 'cn','mail']
    };

    const searchResult = await client.search(ldapConfig.searchBase, searchOptions);
    console.log("Here's your entries:", searchResult);
    return searchResult;
  } catch (e) {
    // console.error(`Search error: ${e}`);
    if (e instanceof Error) {
		console.error('Error name:', e.name);
		console.error('Error message:', e.message);
		console.error('Error stack:', e.stack);
	} else {
		console.error('Unexpected error:', e);
	}
    return [];
  }
}

(async () => {
  console.log("we're binding clients here...")
  await bindClient();  // Call the binding function

  const searchUid = 'example123'; // Example search UID
  console.log("we're now trying to search for user",searchUid)
  await searchUser(searchUid); // Perform the search
})();

I have tried running the exact same code on my local and it works perfectly. I have also added the npm package of ldapjs-client in my activepieces. Somehow it gives me the following error:

 {"stack":"TypeError: e.code is not a function\n    at Object.<anonymous> (webpack://activepieces/engine/src/lib/core/code/no-op-code-sandbox.ts:8:27)\n    at Generator.next (<anonymous>)\n    at <anonymous> (webpack://activepieces/node_modules/tslib/tslib.es6.mjs:121:69)\n    at new Promise (<anonymous>)\n    at Module.f (webpack://activepieces/node_modules/tslib/tslib.es6.mjs:117:10)\n    at Object.runCodeModule (/usr/src/app/cache/main.js:1:460958)\n    at <anonymous> (webpack://activepieces/engine/src/lib/handler/code-executor.ts:46:42)\n    at Generator.next (<anonymous>)\n    at a (webpack://activepieces/node_modules/tslib/tslib.es6.mjs:118:56)","message":"e.code is not a function"}

I then proceed to just clear out everything and write just

console.log("hello world :D") 

its also giving me the same error.

What is wrong with this code piece? Can someone please help with this because its kinda urgent?

Thanks!

The code requires a default code function to run successfully, but it is missing from your code.

export const code = async (inputs) => {
  console.log("we're binding clients here...")
  await bindClient();  // Call the binding function

  const searchUid = 'example123'; // Example search UID
  console.log("we're now trying to search for user",searchUid)
  await searchUser(searchUid); // Perform the search
};

Try running the code after adding the missing code function. Let me know if you encounter any errors.

Try running the code as follows:

export const code = async (inputs) => {
  console.log("HELLO WORLD")
};```
1 Like

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