0

I am making a call to a backend API which returns a JSON response. I have read a number of answers on here including this but they dont seem to cover my issue.

The JSON response from the API looks like this:

[
    {
        "id": "1", 
        "name": "test1",
    },
    {
        "id": "2",
        "name": "test2",
    }
]

My Angular JS code looks like this:

controller.testDetailHolder = [];

Test.getAllTests.query({}, function(data){
            for(var i=0; i<data.length;i++){
                controller.testDetailHolder.name = data[i].name;
                controller.testDetailHolder.id = data[i].id;
            }   
        });

When I console.log controller.testDetailHolder it only shows the second value from the GET request (i.e. the array just holds one value) however, I want the controller.testDetailHolder to look like this:

controller.testDetailHolder = [
    {
        id: "1", 
        name: "test1",
    },
    {
        id: "2",
        name: "test2",
    }
]

Any assistance with this would be greatly appreciated.

Thanks.

0

5 Answers 5

1
controller.testDetailHolder = [];

Test.getAllTests.query({}, function(data){
            for(var i=0; i<data.length;i++){
                controller.testDetailHolder.push({
                    name : data[i].hours,
                    id   : data[i].id
                });
            }   
        });
Sign up to request clarification or add additional context in comments.

Comments

1

Your loop is constantly overwriting controller.testDetailHolder, so you need to use array syntax:

Test.getAllTests.query({}, function(data){
            for(var i=0; i<data.length;i++){
                controller.testDetailHolder[i].name = data[i].hours;
                                           ^^^ 
                controller.testDetailHolder[i].id = data[i].id;
            }   
        });

2 Comments

hey simon, thanks for your help on this... everyone has given the same answer but as you were the first I wanted to ask --- it is saying Cannot set property 'hours' of undefined --- any idea why I am getting this error?
In your situation I would use devtools to indirect the data object being returned
1

You can assign the data like:

Test.getAllTests.query({}, function(data){
  controller.testDetailHolder = data;
});

Or use push to add it to the existing array:

Test.getAllTests.query({}, function(data){
  Array.prototype.push.apply(controller.testDetailHolder, data);
});

Comments

1

controller.testDetailHolder is the array itself, not an element within the array. You want to add the values as elements, which is accessed by controller.testDetailHolder[i], where i is the index of the element. Modify your code to be:

controller.testDetailHolder[i].name = data[i].hours;
controller.testDetailHolder[i].id = data[i].id;

Comments

1

The problem is that you are completely overwriting the complete testDetailHolder array, because you do not specify an index when setting the values.

controller.testDetailHolder.name should be controller.testDetailHolder[i].name


Another issue is that you are using data[i].hours when hours is not defined on the JSON.


Lastly since you want to copy the whole object you could just do a single assignment

Test.getAllTests.query({}, function(data){
        controller.testDetailHolder = data;  
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.