0

Trying to manipulate the response.data to add isLoggedin = true in the response using .map but its giving me an error of "map" is not a function.

vm.dataResponse = response.data.map(function(data){
  data.isLoggedIn = false;
});

Trying to do this so I can have a data to reference whether the user is logged in or not to show (login/sign out). Or I could use $rootScopebut I thought using global is not ideal?

    $rootScope.isLoggedIn = false;

then in my vm.login function the $rootScope will then be set to true, this works but when I refresh the page it goes back to false.

1
  • What it console.log(response.data) prints? Commented Jul 27, 2016 at 0:33

1 Answer 1

1

response.data will need to be an array in order to use .map

.map will create a new array by iterating through all the values of an existing array and returning the results of a provided callback on each member of the array, eg.

var oldArray = [1,2,3,4];
var newArray = oldArray.map(function(number){
    return number * 2;
});
//newArray = [2,4,6,8]

So in your example it wouldn't work even if response.data was an array as you are not returning anything from the callback.

If response.data is an object you can just add the property direct, eg.

response.data.isLoggedIn = false;

If you could do as developer033 suggests and provide the output of a console.log of response.data that would help identify how best to handle it.

Edit:

Because you are just returning a string try:

vm.dataResponse = {username: response.data, isLoggedIn: true}

Sign up to request clarification or add additional context in comments.

1 Comment

Ah I see, I'm only returning a single object when vm.login is called. Just returning the username. Tried just doing response.data.isLoggedIn = false but error says (can't create property on string "username")

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.