0

REPOSITORY METHOD

public int CalculateSoundVolume(string roomName, int currentUser)
{

        {

            //Business Logic

            return finalApplauseVolume;  //**say returning 75**

        }
        catch (Exception ex)
        {
            _logger.LogException(ex);
            throw;
        }                                                                                                                          
 }

WEB API CONTROLLER

    public IHttpActionResult CalculateSoundVolume()
    {
        try
        {
            //Some Logic 
             var result = _applauseRepository.CalculateSoundVolume(huddleName, currentUser);
            return Ok(result); // **it returns 75 here in result**
        }
        catch (Exception ex)
        {
            _logger.LogException(ex);
            throw;
        }
    }

CLIENT SIDE CONTROLLER (ANGULAR JS)

 public calculateSoundVolume() 
 {
    var promise = this.applauseService.calculateSoundVolume();
    promise.then((res) => {
        this.$log.debug("Sound Volume : ", res);
        // Getting Resource [0] : '7' and [1] : '5'
    });
 }

SERVICE

   calculateSoundVolume() 
   {
    return this.soundVolume.get().$promise;        
    }

Now here the scenario is i am returning an integer value from my Repository method. (say 75). I the WEB API controller the value is recieved as 75 in result. But the issue is in "res" in my client side controller i am recieving a Resource as [0]:'7' and [1]: '5' i.e the actual and expected value is not recieved. Please suggest any solution

3
  • Try to send a valid json object from server instead of integer and see if it works. Commented Apr 21, 2014 at 10:58
  • @Chandermani an integer is a valid JSON object. Commented Apr 21, 2014 at 11:58
  • @BenjaminGruenbaum never tried to send integer, like this so i just asked him to try. Now that i try to do a JSON.stringify(1) well does does sealize. Commented Apr 21, 2014 at 12:12

1 Answer 1

2

This same issue was happening to me. Turns out the $promise, instead of returning the int, returns an object that breaks the digits of the integer into different indexes in an array along with some other information the promise uses. I was able to resolve the issue by wrapping the integer with a JObject and passing that from the Web API instead of the integer.

So your Web API would look like this:

public JContainer CalculateSoundVolume()
{
    try
    {
        //Some Logic 
         var result = new JObject(new JProperty("soundVolume", _applauseRepository.CalculateSoundVolume(huddleName, currentUser)));
        return result;
    }
    catch (Exception ex)
    {
        _logger.LogException(ex);
        throw;
    }
}

and your client side controller would change to this:

public calculateSoundVolume()
{
     var promise = this.applauseService.calculateSoundVolume();
     promise.then((res) => {
         this.$log.debug("Sound Volume : ", res.soundVolume);
});

Hope that helps

-- Edit --

I should clarify that I am using the Newtonsoft library for JSON in the above code. I found another stackOverflow question which relates Here. In conclusion Chandermani is correct that you should try sending a valid JSON object back from the server instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Stumbled on this for a few hours. In my case I returned a Java object from the REST service containing only the Integer I wanted to return.

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.