How to return the promise from my function?

Hi there,

beginner Corvid/Javascript user here.

I wrote some code for the front end that searches for a user in a database and calculates his user level based on the number of payments he has made and the time that has passed since his purchase.

Anyway, I defined all my helper functions and tested them but when I call the main function in my $w.onReady() function it only returnss “undefined”. I think I made a mistake returning the promise userLevel from the function returnUserLevel().

Hope someone can point me in the right direction.

// Imports---------------
import wixUsers from 'wix-users';
import wixData from 'wix-data';

// Constants-------------
let currentUserID = wixUsers.currentUser.id;

// Functions-------------
  $w.onReady(function () {
    returnUserLevel(currentUserID).then(level => { 
      console.log(level);
    })
  });

function returnUserLevel (idToCheck) { // Function to return the current user level
  wixData.query("planEvents") // query planEvents
  .eq("userID", idToCheck) // search for the current user ID
  .find()
  .then ( (results) => {
 if (results.items.length > 0){ //if there is an database object with the currentUserID
 let maxUserLevel = results.items[0].maxUserLevel; // extract all relevant information from the object
 let currentPlanValidFrom = results.items[0].currentPlanValidFrom;
 let currentPlanValidFor =results.items[0].currentPlanValidFor;

 let today = new Date();
 let passedMonths = monthDiff(currentPlanValidFrom, today);
 let userLevel = maxUserLevel - (currentPlanValidFor - passedMonths);

 return userLevel 

    } else {
      console.log("The current user is not in the database!");

 return 0;
    }
    })
  .catch ((err) => {
 let errorMsg = err
    console.log(errorMsg)
  }) 
}   

function monthDiff(d1, d2) { // Function calculate the passed months since purchase of the plan
 let months;
  months = (d2.getFullYear() - d1.getFullYear()) * 12;
  months -= d1.getMonth();
  months += d2.getMonth();

 return months < 0 ? 0 : months+1;
}

function unlockLevels(userLevel) { // Function to unlock the different user elements on the page.
 if (userLevel >= 1) {
    $w('#level1').show();
    $w('#placeholder1').hide();
  }

 if (userLevel >= 2) {
    $w('#level2').show();
    $w('#placeholder2').hide();
  }
}

Hello.
I don’t understand what you want to achieve. Are you taking user information from the default database for member data??? Or are you taking it out from a custome database??? I can’t believe that you are not using backend code. I am not an expert at backend-coding. @ahmadnasriya , could you help Gilbert?
Arthur :sweat_smile:

Hi Gilbert :raised_hand_with_fingers_splayed:

You need to return the query as well, it’s like the pyramid, you need to return everything from the top scoop to the end of the nested functions, example:

function getDetails() {
    return wixData.query('class').find().then((x) => {
        let class = x.items[0];
        
        return wixData.query('students').eq('class', class.title).find().then((students) => {
            let student = students.items[0];
            
            return student.name;
        })
    })
}

Every find() function returns a Promise that resolves to the results found by the query and some information about the results, to return a value from the body of that function you need to return the value of the promise.

The student name value will be returned to the find() function as its value, so if you didn’t include a return word before the last find() function, the student name value will stuck there, and won’t be returned to the upper scoop.

To learn more about the promises, please visit this page.

Hope this helps~!
Ahmad