I'm storing a user object on a Cookie and, when the client visit the site again, I want to use this object's properties.
我在Cookie上存储用户对象,当客户端再次访问该站点时,我想使用此对象的属性。
But, when the client comes back, my cookie object looks like [object object]
while it's properties looks like undefined
.
但是,当客户端回来时,我的cookie对象看起来像[object object],而它的属性看起来像undefined。
Here is my code:
这是我的代码:
$scope.signup = function (user) {
$http.post('/api/signup', user)
.then( function (res) {
$cookies.put('user', res.data.user); //This is where I store my cookie.
}, function (res) {
});
};
When the client comes back, I have this code:
当客户端回来时,我有这个代码:
var lastUser = $cookies.get('user');
if (lastUser) $scope.lastName = lastUser.username;
console.log(lastUser); // [object object]
console.log(lastName); // undefined
What can I do to properly get the cookie info?
我该怎么做才能正确获取cookie信息?
1 个解决方案
#1
2
Try instead:
window.localStorage.setItem('user', JSON.stringify(res.data.user));
Then you can get that information on consequent visits with:
然后,您可以通过以下方式获取有关后续访问的信息:
var userData = JSON.parse(window.localStorage.getItem('user'));
That way you'll avoid a lot of the cookie headache.
这样你就可以避免很多令人头疼的问题。
#1
2
Try instead:
window.localStorage.setItem('user', JSON.stringify(res.data.user));
Then you can get that information on consequent visits with:
然后,您可以通过以下方式获取有关后续访问的信息:
var userData = JSON.parse(window.localStorage.getItem('user'));
That way you'll avoid a lot of the cookie headache.
这样你就可以避免很多令人头疼的问题。