Cannot return value from function

I cannot figure out why this function does not return data when it is called. The data is returned in the function itself however. This is probably a simple thing that I am overlooking.

export function returnMemberData ( email )
{

wixData . query ( “Members/PrivateMembersData” )
. eq ( “loginEmail” , email )
. limit ( 1000 )
. find ()
. then ( results => {
if ( results . items . length > 0 )
{
console . log ( “Results found” )
const myResults = results . items [ 0 ];
//console.log("MyResults: " + myResults.firstName)

	 **const**  myUser  = { firstName  :  myResults . firstName , 
                          			lastName  :  myResults . lastName 

}
console . log ( "MyUser: " + myUser . firstName )
return myUser
}
else
{
console . log ( “No Results” )
}
})
. catch ( ( err ) => {
let errorMsg = err ;
console . log ( "Error: " + errorMsg )
} );

}

Now from within my program, I enter this:

var obj = returnMemberData ( “len@perroots.com” )
console . log ( "First Name: " + obj . firstName )

firstName is underlined in red with a message: Property ‘firstName’ does not exist on type ‘void’

And when I run the program in Preview, I get this error in console:
TypeError: Cannot read property ‘firstName’ of undefined

What am I doing wrong? Thanks

Your code is perfect except one small error. So in .then() the response gives a return to wixData.query and not the main function i.e returnMemberData. You thus need to return wixData.query as well. Here is the code:


import wixData from 'wix-data';

export function returnMemberData(email) {

 return wixData.query("Members/PrivateMembersData")
 .eq("loginEmail", email)
 .limit(1000)
 .find()
 .then(results => {
 if (results.items.length > 0) {
                console.log("Results found")
 const myResults = results.items[0];
 //console.log("MyResults: " + myResults.firstName)

 const myUser = {
                    firstName: myResults.firstName,
                    lastName: myResults.lastName
 }
                console.log("MyUser: " + myUser.firstName)
 return myUser
 } else {
                console.log("No Results")
 }
 })
 .catch((err) => {
 let errorMsg = err;
            console.log("Error: " + errorMsg)
 });
}

I hope this was helpful.

Thanks for your reply. I updated the code as indicated above, but still get an error.