In the console my code is printing:
[Object, Object]
I can't drill down normally.
I have tried JSON.Parse and JSON.stringify without success.
My service that sets credentials:
setCredentials.js
'use strict';
// service that handles setting and getting cookies
app.service('setCredentials', function($cookies) {
// function that gets json object and stores it inside a cookie
this.storeInCookie = function(responseObject) {
console.log(responseObject);
//set the cookie
var cookieObj = {
currentUser: {
userType: responseObject.auth.userType,
username: responseObject.auth.email,
token: responseObject.auth.token
}
};
console.log(cookieObj);
//set up header, in case we need auth token inside it for later
//$http.defaults.headersmon['Authorization'] = 'Basic ' + authdata;
//store inside of cookie
$cookies.put('globals', cookieObj);
return true;
};
//function to remove cookie
this.removeCookie = function() {
};
//function to get cookie
this.getCookie = function(cookieName) {
//get cookie
return $cookies.get(cookieName);
};
});
I then call the cookie object in a controller:
navigationController.js
'use strict';
//app global variable
//this is the controller that handles post requests
//declare services as dependecies $http, $location, custom service apiServiceWeb
app.controller('navigationController', function($scope, $rootScope, $http, $location, $cookies, setCredentials) {
//navigation menu
//get what is in the cookie
var cookieValue = setCredentials.getCookie('globals');
console.log($cookies.get('globals'));
});
In the console my code is printing:
[Object, Object]
I can't drill down normally.
I have tried JSON.Parse and JSON.stringify without success.
My service that sets credentials:
setCredentials.js
'use strict';
// service that handles setting and getting cookies
app.service('setCredentials', function($cookies) {
// function that gets json object and stores it inside a cookie
this.storeInCookie = function(responseObject) {
console.log(responseObject);
//set the cookie
var cookieObj = {
currentUser: {
userType: responseObject.auth.userType,
username: responseObject.auth.email,
token: responseObject.auth.token
}
};
console.log(cookieObj);
//set up header, in case we need auth token inside it for later
//$http.defaults.headers.mon['Authorization'] = 'Basic ' + authdata;
//store inside of cookie
$cookies.put('globals', cookieObj);
return true;
};
//function to remove cookie
this.removeCookie = function() {
};
//function to get cookie
this.getCookie = function(cookieName) {
//get cookie
return $cookies.get(cookieName);
};
});
I then call the cookie object in a controller:
navigationController.js
'use strict';
//app global variable
//this is the controller that handles post requests
//declare services as dependecies $http, $location, custom service apiServiceWeb
app.controller('navigationController', function($scope, $rootScope, $http, $location, $cookies, setCredentials) {
//navigation menu
//get what is in the cookie
var cookieValue = setCredentials.getCookie('globals');
console.log($cookies.get('globals'));
});
Share
Improve this question
edited Aug 18, 2015 at 13:50
maxshuty
10.7k13 gold badges69 silver badges86 bronze badges
asked Aug 18, 2015 at 13:36
user3754111user3754111
8691 gold badge10 silver badges20 bronze badges
1
-
Please add the
$log
dependency in your service declaration:app.service('setCredentials', function($cookies, $log) ...
, then you can use advanced logging, like:$log.debug(responseObject);
. Hope this helps. – barfoos Commented Aug 18, 2015 at 13:53
1 Answer
Reset to default 17Use
$cookies.putObject('globals', cookieObj)
and
$cookies.getObject('globals')
instead.