2

I am making a login app in ionic using PHP, I have a login function which works well but I need to be able to store certain values to local storage for later use.

Here is a sample from my code:

$scope.login = function() {
    Login.login($scope.login).then(function (data) {
        if (Object.keys(data.data).length === 1) {
            var datao = angular.toJson(data);
            $scope.profile = datao;
            $scope.email = $scope.profile.email;
            $scope.username = $scope.profile.username;
            console.log(data);
            console.log($scope.profile);
              // $state.go('dashboard');
        }
        ....
    });
    ....
});

$scope.profile displays the following:

[Log]
     {
        "data": [{
            "id": "5",
            "username": "ozombo",
            "reputation": "ceo",
            "activate": "8425",
            "followercount": "3",
            "praycount": "0",
            "donetour": "0",
            "fb_id": "0",
            "fullname": "Adebambo Oyelaja",
            "church": "RCCG ",
            "photo": "1417451697.jpg",
            "twtr_id": "0",
            "screen_name": "",
            "email": "[email protected]",
            "bio": "Geek, Father, Husband",
            "country": "Nigeria",
            "state": "Lagos",
            "followscount": "0",
            "account": "",
            "password": "5cf1bc1b9a2ada1ae9e29079aae1aefa",
            "signup_date": "1413305984",
            "signupdevice": "web",
            "keycode": "5fc868f0767b0c164341a9e8e35edbe4",
            "last_login": "1436519269",
            "city": "Palmgroove",
            "campaign": "",
            "bday": "29-09-1988",
            "gender": "Male",
            "approved": "0",
            "donewelcome": "0",
            "phonenumber": "07017993683",
            "secure": "private",
            "churchid": "1",
            "views": "68",
            "marital": "Married"
        }],
        "status": 200,
        "config": {
            "method": "GET",
            "transformRequest": [null],
            "transformResponse": [null],
            "url": "http://localhost:8888/opb/api/[email protected]&pass=oyelaja",
            "headers": {
                "Accept": "application/json, text/plain, /"
            }
        },
        "statusText": "OK"
    }

How will I get the user email for example

2 Answers 2

1

You can get the email by accesing to data.email like var email = $scope.profile.data.email

You forgot that you have data before you can get the email.

{
  "data": [{
    "username": "ozombo",
    "email": "[email protected]",
    ...
  }]
}
Sign up to request clarification or add additional context in comments.

6 Comments

var email = $scope.profile.data.email isn't working... Error: undefined is not an object (evaluating '$scope.profile.data.email')
if $scope.profile logs what you shows in the post you can acces like i said
Yes my logs show what i posted but when i do this: $scope.profile = datao; console.log($scope.profile.data.email); i get Error: undefined is not an object (evaluating '$scope.profile.data.email')
Could you please try $scope.profile.data[0] ? From what I see, your data object is actually a table, containing objects.
@trichetriche var email = $scope.profile.data[0]; gives undefined is not an object fernando data displays {data: Array, status: 200, headers: function, config: Object, statusText: "OK"}
|
1

You can get access to the user email by iterating over the data array:

$scope.profile.data.forEach(function(d) {
    console.log(d.email);
});

Hope that helps!

5 Comments

after doing this, i get Error: undefined is not an object (evaluating '$scope.profile.data.forEach')
There isn't any way that this can be the case if what you provided in the question is accurate. Can you do console.log($scope.profile.data); and show your output?
undefined is what i get
This should be showing your first user. What does console.log($scope.profile); give you?
[Log] {"data":[{"id":"5","username":"ozombo","reputation":"ceo","activate":"8425","followercount":"3","praycount":"0","donetour":"0","fb_id":"0","fullname":"Adebambo Oyelaja","church":"RCCG ","photo":"1417451697.jpg","twtr_id":"0","screen_name":"","email":"[email protected]","bio":"Geek, Father, Husband","country":"Nigeria","state":"Lagos","followscount":"0","account":"","password":"5cf1bc1b9a2ada1ae9e29079aae1aefa","signup_date":"1413305984","signupdevice":"web","keycode":"5fc868f0767b0c164341a9e8e35edbe4","last_login":"1436519269", please refer to my question for full LOG

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.