0

I don't understand how the $http service works. I have a controller that calls a factory and the factory calls a WS function.

If I run the app on debug mode, the factory calls the WS function and the WS function returned what I suppose is an json object.

On the other hand, when I watch in the web console, I get a message "undefined". What should return the WS function? Or what's wrong in my factory?

Thanks in advance.

This is the VB.Net Web Service function:

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
<WebMethod()> _
Public Function getLogin(ByVal email As String, ByVal password As String) As String
        Dim dt As New Data.DataTable
        Dim result As String = ""
        Using con As New SqlConnection(sConn)
            'Dim sql As String = "Select id_usuario, nombre_usuario id_tipo_usuario from binder_usuarios where email = @email and password = @password"
            Dim sql As String = "Select * from binder_usuarios where email = @email and password = @password"
            Dim cmd As New SqlCommand
            cmd.CommandText = sql
            cmd.Connection = con
            cmd.Parameters.AddWithValue("@email", email)
            cmd.Parameters.AddWithValue("@password", password)
            Dim dataAdapter As New SqlDataAdapter(cmd)
            dataAdapter.Fill(dt)
            If dt.Rows.Count > 0 Then
                result = JsonConvert.SerializeObject(dt, New JavaScriptDateTimeConverter())
                Return result
            Else
                result = "e"
            End If
            Return result
        End Using
    End Function

the WS function return a string:

"[{"id_usuario":3,"nombre_usuario":"Quethzel","apellido_paterno":"Diaz","apellido_materno":"Zarate","id_area":1,"id_tipo_usuario":1,"password":"sa","email":"hqdiaz@lis.com.mx"}]"

this is the factory:

function fcQAuth($http, $q, $window) {
    return {
        loginAuth: function(credentials) {
            $http.post('../ws/wsReact.asmx/getLogin', credentials)
            .success(function(data, status) {
                return "your data it's Ok, in da face !";
            })
            .error(function(data, status) {
                return "don't give up Nigga";
            });

            //return "myUser. " + credentials.email + ", myPass." + credentials.password;
        }
    }
}; // end fcQAuth factory

And this is the controller, that call the factory:

function loginCtrl($scope, $q, $http, $location, fcQAuth) {

    $scope.loginUser = function() {
        var credentials = {
            email: $scope.user.email === '' || $scope.user.email === undefined ? 0 : $scope.user.email,
            password: $scope.user.password === '' || $scope.user.password === undefined ? 0 : $scope.user.password
        };
        $scope.userInfo = fcQAuth.loginAuth(credentials);
        console.log($scope.userInfo);
    };
};
Darkmouse
  • 1,941
  • 4
  • 28
  • 52
Quethzel Diaz
  • 621
  • 1
  • 11
  • 26
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – charlietfl Mar 08 '15 at 22:16

1 Answers1

0

The http.post is an asyncronous call, when the console.log in your controller is executed, the http.post has no returned yet. Try this:

Return the promise in your factory:

function fcQAuth($http, $q, $window) {
    return {
        loginAuth: function(credentials) {
            return $http.post('../ws/wsReact.asmx/getLogin', credentials);
        }
    };
}

Get the data in the success callback in your controller:

function loginCtrl($scope, $q, $http, $location, fcQAuth) {

        var credentials = {
            email: $scope.user.email === '' || $scope.user.email === undefined ? 0 : $scope.user.email,
            password: $scope.user.password === '' || $scope.user.password === undefined ? 0 : $scope.user.password
        };
        fcQAuth.loginAuth(credentials).then(function(data){
             $scope.userInfo = data;
             console.log($scope.userInfo);
        });
};
cuttlas
  • 971
  • 5
  • 17