im trying to update database record with function return Ionic, Back&

82 Views Asked by At

Hi I'm trying to update my database with function that returns a number

$scope.sum = function()
{ 
    return  $scope.inp + $scope.points; 
};

this function will update the record in object points, column name and id 1:

$scope.addPoint = function() {

    PointService.addPoint($scope.sum, 1)
    .then(function(result) {
      $scope.inp = 0;

      getMyPoints();
    });
}

addPoint = function(id,points) 
{
    return $http.put(getUrlForId(1),points,name);
}

the error is: Error details: Cannot convert type 'int' to 'System.Collections.Generic.Dictionary'

the data type of the field is Float. Any idea what is wrong with the code?

2

There are 2 best solutions below

1
Junaid Sarwar On

you are passing function reference to PointService.addPointer(), use this:

$scope.addPoint = function() {

    PointService.addPoint($scope.sum(), 1) // NOT PointService.addPoint($scope.sum, 1) 
    .then(function(result) {
      $scope.inp = 0;

      getMyPoints();
    });
}

this will execute your function and pass the output (id parameter) to addPoint function, further, for more safe side, you can return Number from $scope.sum() i.e.

$scope.sum = function()
{ 
    return  Number($scope.inp + $scope.points); 
};

0
Matt Billock On

This looks like an issue with how you're contacting Backand. You use the following code to send your points over:

addPoint = function(id,points) 
{
    return $http.put(getUrlForId(1),points,name);
}

This is an older version of calling the Backand API that is manually constructing a PUT request, and putting "points" and "name" as the "Data" and "config" parameters to $http. With an object update via PUT, you'll need to provide the updates as an object. So if you wanted to update the points and the name of the object (and I'm doing some assuming based upon what I can tell from the code snippet above), you'd need to encapsulate these properties in an object that has the following general format:

{
  "field_name_1":"new value_1",
  "field_name_2":"new value_2",
  etc ...
}

This should then be sent as the body of the request. So, for your code, change it to the following and see if this helps:

addPoint = function(id,points) 
{
    return $http.put(getUrlForId(1),{points: points, name: name});
}

To give more info on why you're seeing this particular error, Backand is depending on this JSON format in the body. Our platform should definitely do more validation (and I'll create a ticket for the devs to handle non-conforming input more gracefully), but at the moment we simply take the body of the request, convert it to a dictionary object, then begin the requested operation. As your code above sends only "1.0" as the body, this fails the conversion into a dictionary, causing the stack exception and the issue you are seeing.

As a note, we offer a new SDK that encapsulates these HTTP methods, performing the authentication header generation and HTTP messaging for you, providing promises to handle responses. You can find it on our Github page at https://github.com/backand/vanilla-sdk. To make the same call using the new SDK, the code would resemble the following:

backand.object.update("your object name", 1, {name: name, points: points})
  .then(function(response){
    console.log(response.data);
  });