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

  • SEARCH
  • Home
  • 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 8650087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:43:46+00:00 2026-06-12T13:43:46+00:00

i write some function, and i called abApi.general.getUserById function, but when i print output

  • 0

i write some function, and i called abApi.general.getUserById function, but when i print output to $("#friend-requests-block") my name variable is empty. i know this is javascript and i try with $("#friend-requests-block") in getUserById callback, but then i haven’t type. How can i fix this?

                for (var index in requests) {

                        var name = "";
                        var type = "";

                        if(requests[index].FriendRequestTypeId == 1) {
                            type = "private";
                        }
                        else {
                            type = "business";  
                        }

                        abApi.general.getUserById(abEnvironment.sessionToken, requests[index].FromUserId, function(response2){
                            name = response2.Name;  
                        });

                        $("#friend-requests-blocks").html($("#friend-requests-blocks").html() + "<div class=\"log-block\" id=\"friend-requests-log-"+requests[index].Id+"\"><a href=\"#\"><h2>"+ name + "("+ type +")</h2></a> <div class=\"friend-requests-buttons\" style=\"margin-top: 15px;\"> <a href=\"javascript:void(0)\" data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 1,"+requests[index].FromUserId+");\">Accept</a><a href=\"javascript:void(0)\"data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 0,"+requests[index].FromUserId+");\">Reject</a> <a class=\"button3\" href=\"javascript:void(0)\"data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 2,"+requests[index].FromUserId+");\">Later</a></div></div>");    

                }   
  • 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-12T13:43:47+00:00Added an answer on June 12, 2026 at 1:43 pm

    It’s not just a scope issue you’re dealing with, but the fact that getUserById is async, too (even more than the scope). the variable name won’t be set until your script receives a response. Having said that, since you’re using a loop, moving the $('#friend-request-blocks').html() bit to the success callback won’t cut it: the variables name and type will be reassigned on every loop iteration. To get around this, you’ll have to use a closure:

    abApi.general.getUserById(abEnvironment.sessionToken,requests[index].FromUserId,
    (function(type)
    {//pass the current type as an argument to closure
        return function(response2)
        {
            var name = response2.Name;//declare local variable name, or use response2.Name
            $('#friend-request-blocks').html('html goes here with correct type: '+type+' and name: '+name);
        };
    }(type)));
    

    Also, I get the impression you’re looping through an array, not an object, though I could be wrong. If requests is an array, it’s best not to use a for...in loop, but a regular for(var i;i<requests.length;i++) loop. Google will give you a huge list of reasons as to why for...in on arrays isn’t the best of ideas.

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

Sidebar

Related Questions

I'm trying to write a Counting sort function but for some reason it's getting
Hey i'm just trying to write some code in DrScheme: ((function (x) (* x
Is there some way to write a function f :: (a -> b ->
I need to write a delegate function that can 'wrap' some while/try/catch code around
I'd like to write a function that would have some optional code to be
I'm trying to write a javascript function that adds some DOM nodes to the
I'm trying to write a recursive function that would get some string, as well
I need some help in getting this right, problem Write a function which takes
Let's say I have a function called DisplayWhiskers() which puts some slashes and backslashes
I have some expensive function f(x) that I want to only calculate once, but

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.