Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:33:54+00:00 2026-06-15T09:33:54+00:00

I have a table called Subscription and another table called Client I need the

  • 0

I have a table called Subscription and another table called Client I need the gender of the Client who owns the subscription every time I make an update. Here’s my update script:

    function update(item, user, request) {
    var subscriptionId = item.id;
    var subscriptionActivitiesTable = tables.getTable("SubscriptionActivity");
    var userTable = tables.getTable("User");
    var activityTable = tables.getTable("Activity");
    var userGender = userTable.where({id: item.UserId}).select('Gender').take(1).read();
    console.log(userGender);
    activityTable.where({PlanId:item.PlanId, Difficulty: item.Difficulty}).read({
         success: function(results){
             var startDate = item.StartDate;
             results.forEach(function(activity)
             {
                var testDate = new Date(startDate.getFullYear(),startDate.getMonth(), startDate.getDate());
                testDate.setDate(testDate.getDate() + activity.Sequence + (activity.Week*7));
                subscriptionActivitiesTable.insert({SubscriptionId: subscriptionId, 
                ActivityId: activity.id, ShowDate: new Date(testDate.getFullYear(), 
                    testDate.getMonth(), testDate.getDate()), CreationDate: new Date()});

             })
         }
     });

     var planWeeks = 12;//VER DE DONDE SACAMOS ESTE NUMERO
     var idealWeight = 0;
     if (userGender === "Male")
     {
        idealWeight = (21.7 * Math.pow(parseInt(item.Height)/100,2));    
     }
     else
     {
         idealWeight = (23 * Math.pow(parseInt(item.Height)/100,2));  
     }

     var metabolismoBasal = idealWeight * 0.95 * 24;
     var ADE = 0.1 * metabolismoBasal;
     var activityFactor;
     if (item.Difficulty === "Easy")
     {
         activityFactor = 1.25;
     }
     else if(item.Difficulty === "Medium")
     {
         activityFactor = 1.5;
     }
     else
     {
         activityFactor = 1.75;
     }
     var caloricRequirement = ((metabolismoBasal + ADE)*activityFactor);
     activityTable.where(function(item, caloricRequirement){
         return this.PlanId === item.PlanId && this.Type != "Sport" && 
         this.CaloricRequirementMin <= caloricRequirement && 
         this.CaloricRequirementMax >= caloricRequirement;}, item, caloricRequirement).read({
         success: function(results)
         {
             var startDate = item.StartDate;
             results.forEach(function(activity)
             {
                for (var i=0;i<planWeeks;i++)
                {
                     var testDate = new Date(startDate.getFullYear(),startDate.getMonth(), startDate.getDate());
                     testDate.setDate(testDate.getDate() + activity.Sequence + (i*7));
                     subscriptionActivitiesTable.insert({SubscriptionId: subscriptionId, 
                     ActivityId: activity.id, ShowDate: new Date(testDate.getFullYear(), 
                     testDate.getMonth(), testDate.getDate()), CreationDate: new Date()});
                }
             })
         }
     })
     request.execute();
}

I tried the code above and clientGender is undefined. As you can see I want to use the gender to set the idealWeight.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:33:56+00:00Added an answer on June 15, 2026 at 9:33 am

    The read() method expects a function to be passed in on the success parameter – it doesn’t return the result of the query like you’d think.

    Try something like this instead:

    function update(item, user, request) {
        var clientTable = tables.getTable("Client");
        var clientGender = 'DEFAULT';
        clientTable.where({id: item.ClientId}).select('Gender').take(1).read({
            success: function(clients) {
                if (clients.length == 0) {
                    console.error('Unable to find client for id ' + item.ClientId);
                } else {
                    var client = client[0];
                    clientGender = client.Gender;
    
                    // since we're inside the success function, we can continue to 
                    // use the clientGender as it will reflect the correct value
                    // as retrieved from the database
                    console.log('INSIDE: ' + clientGender);
                }
            }
        });
    
        // this is going to get called while the clientTable query above is
        // still running and will most likely show a value of DEFAULT 
        console.log('OUTSIDE: ' + clientGender);
    
    }
    

    In this sample, the client table query is kicked off, with a callback function provided in the success parameter. When the query is finished, the callback function is called, and the resulting data is displayed to the log. Meanwhile – while the query is still running, that is – the next statement after the where/take/select/read fluent code is run, another console.log statment is executed to show the value of the clientGender field outside the read function. This code will run while the read statement is still waiting on the database. Your output should look something like this in the WAMS log:

    * INSIDE: Male
    * OUTSIDE: Default
    

    Since the log shows the oldest entries at the bottom, you can see that the OUTSIDE log entry was written sometime before the INSIDE log.

    If you’re not used to async or functional programming, this might look weird, but as far as I’ve found, this is now node works. Functions nested in functions nested in functions can get kind of scary, but if you plan ahead, it probably won’t be too bad 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table called page which represents every single page in my website. page_id
I have table of users called users and log table called ulog . Every
I have table called 'shipped_data' https://i.stack.imgur.com/JXBej.png and need go get all 'caseNumbers' that match
I have table called users and I want to make an exact copy as
I have table called product product_id product_Name product_Price product_Description product_image category_id another table category
I'm using mysql database. In that I have table called tbl_user I need to
I have table called stats . In am inserting yes or no in the
i have table called as Support, which have a field named Name and contains
I have table called Buttons. Buttons table i have column button_number . Table contain
I have table called posts in my DB. Each post has field called social_network

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.