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

1 Answers1

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.

Community
  • 1
  • 1
David Zehr
  • 70
  • 8
  • 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. – numsu Jul 28 '16 at 12:39