0

In the following sample of code I send a "request" and I am trying to get a response but that returns "undefined" value. This is my code so far

$scope.SameNameFunction = function() {
            var payload = { itemname: $scope.EventDetails.Name};
            portalRepository.namecall(payload).then(function (payload) {
                console.log(payload.valuesreturned);
                alert("Detected: " + payload.valuesreturned + " events having the same name");
            });
        };

Code from the http.post

            namecall: function (payload) {
                return $http.post("/Api/PortalData/NameNumberResult", payload);
            },

Code from the .cs controller:

public ActionResult NameNumberResult(ItemEventNameDTO payload)
{
    var valuetosend = payload.itemname;
    var acf = new AcFunctions();
    var newstorevalue = SqlHelper.ExecuteDataset(acf.AcConn(), "sp_selectbyname", valuetosend);
    payload.valuesreturned = newstorevalue.Tables[0].Rows.Count;
    return payload.GetSuccess();
}

Putting a breakpoint I am getting the appropriate value from the stored procedure, either in .cs and .js files. But while trying to print the message in screen, value do not appear and "Detected undefined events having the same name" is showing instead. Any help is welcome!

5
  • 1
    Can you show the code related to portalRepository.namecall? Commented May 13, 2016 at 11:28
  • @Satpal It is just an http post, I do not think that matters ` namecall: function (payload) { return $http.post("/Api/PortalData/NameNumberResult", payload); },` Commented May 13, 2016 at 11:32
  • @Saptal I do not return a promise. In the description I am writing what exactly I want to do with code, I am not sure about the way to do that, but I think that can be implemented via javascript promises Commented May 13, 2016 at 11:35
  • @GeorgeD I suspect it does matter. I would put it in the code of the question. Commented May 13, 2016 at 11:36
  • You are returning promise mate, so use it Commented May 13, 2016 at 11:39

1 Answer 1

2

As you per your code, you are returning promise

namecall: function (payload) { 
    return $http.post(url, payload); 
}

You need to use the callback method of $http.post()

 portalRepository.namecall(payload).then(function(data){
     alert("Detected: " + payload.valuesreturned + " events having the same name");
 });
Sign up to request clarification or add additional context in comments.

Comments

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.