Support Topics Documentation Slack YouTube Blog

Relations with geo points via api


(Fabio Na) #1

HI I’m trying to save\update a geopoint in my table, I have used also the code in the docs with no result.

https://backendless.com/docs/js/doc.html#data_relations_with_geo_points
with this code no geopoint is created neither. I’ve tried my code, with geoPoint I’ve tryed an existing object geopoint and also only the id of an existing geopoint.

function updateLocation(placeObjectId,geoPoint){
var place = {
    objectId : placeObjectId,
    location : geoPoint,
    phone : "525252"
}

Backendless.Data.of( "Place" ).save( place )

.then( function( savedObject ) {
console.log( “Place instance has been updated” );
})
.catch( function( error ) {
console.log( "an error has occurred " + error.message );
}

);

Am I doing something wrong? It doesnt work how I think it works? thanks


(Mark Piller) #2

Hi Fabio,

I checked the docs and it appears the JS section has not been updated to reflect the changes in the product (the Android and iOS are up to date though. I will get the docs changed asap. Here’s a sample code which shows how to create a relation between a data object and a geopoint.

var geopoint = {latitude:32.77, longitude:96.79}
    Backendless.Geo.addPoint( geopoint )
     .then( function( savedGeoPoint ) {
     	 Backendless.Data.of( "Person" ).findFirst()
          .then( function( firstPerson ) {
            Backendless.Data.of( "Person" ).setRelation( firstPerson, "location:GeoPoint:1", [ savedGeoPoint ] )
             .then( function( count ) {
              console.log( "count is " + count );
             } )
            .catch( function( error ) {
              console.log( "setRelation error " + error );
            })
        })
     })

The code does the following:

  1. Saves a geopoint
  2. Retrieves a data object
  3. Creates a relationship between the data object and the geopoint from (1)

For more details about the setRelation API, please see the API doc: https://backendless.com/docs/js/doc.html#data_relations_api_set_add_js

Regards,
Mark


(Fabio Na) #3

wow man, didn’t expect a solution so fast. It worked perfectly. Is also possible to make a one to many relation? I’ve tryed

Backendless.Data.of( “Place” ).setRelation(parentObjectId, “location:GeoPoint:n”, [mapId1, mapId2])
.then( function( count ) {
console.log( “relation has been set”+count );
})
.catch( function( error ) {
console.log( "server reported an error - " + error.message );
});

console pointed me to use geoPoint:n instead of geoPoint:1 but it didn’t work


(Mark Piller) #4

What you did is correct, but it will work only if the relation column doesn’t exist yet. If the column is already defined as 1:1 and the code uses location:GeoPoint:n, you will see an error. So you have the following options:

  1. Define the relation column in console as one-to-many and use location:GeoPoint in the code

or

  1. Delete the column in console and always use location:GeoPoint:n in the code.

Hope this helps.

Mark


(Fabio Na) #5

Mark, what can I say, thanks, quick and perfect answer. thanks a lot. Keep going guys you rock