Returning value from a function - returns undefined?

I have some functions stored in a backend file, but when I am calling them in an async hook they are returning ‘undefined’.

If I run the functions directly in the hook it works fine, but this is not desired as I want to be able to call the function in other places too.

I am having this issue with multiple functions, also with some which are intended to return arrays of values. All are returning ‘undefined’…

Any help would be appreciated!

See code below:

functions.js:

import wixData from 'wix-data';

export function getPrefix(client) {

    wixData.query("Client")
        .eq("client", client)
        .find()
        .then((results) => {
        
 var clientInfo = results.items[0];

 var prefix = `${clientInfo.prefix}`;

 return prefix;
 
        })
}

beforeInsert async hook:

import { getPrefix } from 'backend/functions';

var client = `${item.client}`;

const prefix = await getPrefix(client);

item.prefix = prefix;

If I run it all together in the hook it works fine:

import wixData from 'wix-data';

var client = `${item.client}`;
        
const prefix = await wixData.query("Client")
        .eq("client", client)
        .find()
        .then((results) => {
        
 var clientInfo = results.items[0];

 var prfx = `${clientInfo.prefix}`;

 return prfx;
 
        });
        
    item.prefix = prefix;

Please help!

Many thanks in advance!

I didn’t go over your entire code but it looks like you’re missing a “return” at the beginning:

  return wixData.query("Client")
  .eq("client", client)//etc...