Reputation: 2097
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 $rootScope
but 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.
Upvotes: 0
Views: 4972
Reputation: 1899
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}
Upvotes: 1