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 8905421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:23:24+00:00 2026-06-15T02:23:24+00:00

I’m working on creating a Users collection with the ability to then grab single

  • 0

I’m working on creating a Users collection with the ability to then grab single users inside. This will be used to match from another system, so my desire is to load the users once, and then be able to fine/match later. However, I’m having a problem accessing the outer users collection from an inner method.

function Users(){

    var allUsers;

    this.getUsers = function () {
        // ajax to that Jasmine behaves    
        $.ajax({
            url: '../app/data/jira_users.json',
            async: false,
            dataType: 'json',
            success: function(data) {
                allUsers = data;
            }
        });
        return allUsers;
    };

    this.SingleUser = function (name) {
        var rate = 0.0;
        var position;

        this.getRate = function () {
            if(position === undefined){
                console.log('>>info: getting user position to then find rate');
                this.getPosition();
            }

            $.ajax({
                url: '../app/data/rates.json',
                async: false,
                dataType: 'json',
                success: function(data) {
                    rate = data[position];
                }
            });
            return rate;
        };

        this.getPosition = function () {
            console.log(allUsers);
            //position = allUsers[name];
            return position;
        };

        //set name prop for use later I guess.
        this.name = name;
    };
}

and the test that’s starting all of this:

it("get single user's position", function(){
    var users = new Users();
    var someone = new users.SingleUser('bgrimes');
    var position = someone.getPosition();
    expect(position).not.toBeUndefined();
    expect(position).toEqual('mgr');
});

The getPosition method is the issue (which might be obvious) as allUsers is always undefined. What I have here is yet another attempt, I’ve tried a few ways. I think the problem is how the Users.getUsers is being called to start with, but I’m also unsure if I’m using the outer and inner vars is correct.

  • 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-15T02:23:25+00:00Added an answer on June 15, 2026 at 2:23 am

    Though the others are correct in that this won’t work as you have it typed out, I see the use case is a jasmine test case. So, there is a way to make your test succeed. And by doing something like the following you remove the need to actually be running any kind of server to do your test.

    var dataThatYouWouldExpectFromServer = {
        bgrimes: {
            username: 'bgrimes',
            show: 'chuck',
            position: 'mgr'
        }
    };
    
    it("get single user's position", function(){
        var users = new Users();
        spyOn($, 'ajax').andCallFake(function (ajaxOptions) {
            ajaxOptions.success(dataThatYouWouldExpectFromServer);
        });
        users.getUsers();
        var someone = new users.SingleUser('bgrimes');
        var position = someone.getPosition();
        expect(position).not.toBeUndefined();
        expect(position).toEqual('mgr');
    });
    

    This will make the ajax call return whatever it is that you want it to return, which also allows you to mock out tests for failures, unexpected data, etc. You can set ‘dataThatYouWouldExpectFromServer’ to anything you want at any time.. which can help with cases where you want to test out a few different results but don’t want a JSON file for each result.

    Sorta-edit – this would fix the test case, but probably not the code. My recommendation is that any time you rely on an ajax call return, make sure the method you are calling has a ‘callback’ argument. For example:

    var users = new Users();
    users.getUsers(function () {
        //continue doing stuff
    });
    

    You can nest them, or you can (preferably) create the callbacks and then use them as arguments for eachother.

    var users = new Users(), currentUser;
    
    var showUserRate = function () {
        //show his rate
        //this won't require a callback because we know it's loaded.
        var rate = currentUser.getRate(); 
    }
    
    var usersLoaded = function () {
        //going to load up the user 'bgrimes'
        currentUser = new users.SingleUser('bgrimes');
        currentUser.getRate(showUserRate);
    }
    
    users.getUsers(usersLoaded);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.