2 entries into table

I am entering data into a table, which has a geopoint relation, as well as a relation to another table. But when I do so, Backendless is creating 2 data entries into the table. One with the relation, and one without. How do I make t so that it only creates one entry with both relations?

Here’s my code:

.controller('AgAddCtrl', function($scope, $state, $http) {
  console.log("Agent Add");
  console.log($state.params.user);
  $scope.hotelReg = function() {
    var address = $scope.hName + ',' + $scope.hLocality + ',' + $scope.hCity;
    var latitude;
    var longitude;
    var user = $state.params.user;
    var url = "https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyCv9qLcpwSqElOvsLVaZNIsY161GU3Y7QY&address=" + address;
    $http.get(url)
      .success(function(data) {
        latitude = data.results[0].geometry.location.lat;
        longitude = data.results[0].geometry.location.lng;
        console.log(latitude);
        console.log(longitude);
        var hotel = {
          name: $scope.hName,
          locality: $scope.hLocality,
          city: $scope.hCity,
          amenities: $scope.hAmenities,
          total_rooms: $scope.hTotalRooms,
          free_rooms: $scope.hFreeRooms,
          latitude: latitude,
          longitude: longitude,
          agent_email: user.email,
          ___class: "Hotels"
        }
        /*Backendless.Data.of("Hotels").save(hotel)
          .then( function( savedObject ) {
              console.log("success");
            })
          .catch( function( error ) {
              console.log("error upload" + error.message);
            });*/
        var point = {
          latitude: latitude,
          longitude: longitude,
          metadata: {Hotels: hotel}
        }
        Backendless.Geo.savePoint( point )
         .then( function( savedGeoPoint ) {
           var child = [ user.objectId ];
           Backendless.Data.of("Hotels").addRelation(savedGeoPoint.metadata.Hotels.objectId,"ag_email",child)
            .then(function(count) {
              console.log("relation set");
            })
            .catch(function(error) {
              console.log("relation error" + err.message);
            });
           console.log( "geo point saved " + savedGeoPoint.objectId );
         })
         .catch( function( error ) {
           console.log( "error - " + error.message );
         });


      })
      .error( function(error) {
        console.log("error map");
      })


  }
})

Hi,

in point metadata you put hotel object without its objectId property, which will return after you save hotel. You should take saved hotel object and put it into point metadata. Then it should work properly for you.

Regards,
Stanislaw

When adding the relation, I am referencing the object ID of the hotel, as shown in this line:

Backendless.Data.of("Hotels").addRelation(savedGeoPoint.metadata.Hotels.objectId,"ag_email",child)

Does this not make sure that the relation is made for that object ID?

Don’t you firstly make Backendless.Data.of(“Hotels”).save(hotel)?