Support Topics Documentation Slack YouTube Blog

Is it possible to retrieve a Geopoint based on its objectId?

Is there a way to retrieve a geopoint by only objectID?

In short what I’m trying to do is this:
My app client can update geopoint through my custom service api (I don’t want to give client full permission to update or remove geopoints).
My service logic will compare uploaded geopoint update with original geopoint to determine if the request is legit(for example if point moved too far).
Therefore I need a way to grab the original geopoint by their objectID to compare with the update

Here are the stuff I tried that didn’t work :sob: :
Backendless.Data.of( “GeoPoints” ).findById( objectId )
Backendless.Persistence.of( Backendless.GeoPoint ).findById(ID)
var geoQuery = {
objectId: name
}
Backendless.Geo.find(geoQuery)

hello @Mian_Wei,

what sdk do you use?

Hi Sergei,

I’m using Js on cloudcode to write the business logic

Hi, @Mian_Wei,

Try this example:
https://backendless.com/docs/js/geo_category_search.html#using-dates-in-where-clause-when-searching-in-categories

Use GeoQuerry for search in the category.
Please, take a closer look at this line: query.condition = "objectId = 'exampleId'"
This condition can be adjusted to any suitable where clause.

Regards, Inna.

Hi Inna,

I tried this, not working either.
And sometimes it seems to returns wrong results :disappointed_relieved:
sometimes error - Invalid where clause
putInStorage( name ) {

console.log(name)

console.log(typeof name)

var query = new Backendless.GeoQuery();
query.categories = [‘breadcrumb’];
query.includeMetadata = true;
query.condition = "objectId = " + name
//tried this too
//query.condition = “objectId = ‘AB193D81-B65A-1C74-FFE8-9E5035382B00’”

return Backendless.Geo.find(query)
.then( function( points ) {
console.log(typeof points)
points.forEach(point => {

        point.categories = ["stored"]
        
        Backendless.Geo.addPoint( point )
              .then( function() {
                console.log( "geo point has been stored" );
               })
                .catch( function( error ) {
     });
     
      })
  
  })

.catch( function( error ) {
console.log( "error - " + error.message );
return "error - " + error.message
});

}

Wed Dec 11 2019 18:52:16 GMT-0500 (EST) | SERVER_CODE | INFO | [31556] ServerCode Model built in 4ms

Wed Dec 11 2019 18:52:16 GMT-0500 (EST) | SERVER_CODE | INFO | [31556] [92CD0C13-F10E-2752-FF59-4F45FD2F5C00] [INVOKE SERVICE] services.trashManager.putInStorage

Wed Dec 11 2019 18:52:16 GMT-0500 (EST) | SERVER_CODE | INFO | [31556] AB193D81-B65A-1C74-FFE8-9E5035382B00

Wed Dec 11 2019 18:52:16 GMT-0500 (EST) | SERVER_CODE | INFO | [31556] string

Wed Dec 11 2019 18:52:16 GMT-0500 (EST) | SERVER_CODE | INFO | [31556] Processing finished in 40.266msPreformatted text

We’ll check you use case and post here the results. But for now i can say, that the searching by category and objecId works fine.
Did you try make minimal example in plain js code – just query with two parameters?

Hi @Mian_Wei

the problem is with missed return keyword before Backendless.Geo.addPoint( point )
also as there can be only one geoPoint with the same objectId we do not need to go through array of points, or you have to collect all the requests and then pass it into Promise.all([....])

let’s summarize

  1. retrieving geo point by id works properly
  2. updating geo points works properly
  3. to be sure that all requests are done before a cloudcode worker closes current invocation you have to keep promises chain, or you can use async/await

here is a working code what I just tested with my app:

putInStorage(name) {
  const query = new Backendless.GeoQuery();
  query.categories = ['breadcrumb'];
  query.condition = `objectId = '${name}'`

  return Backendless.Geo.find(query)
    .then(function(points) {
      if(!points[0]){
         return here an error or whatever you wish
      }

      points[0].categories = ['stored']

      return Backendless.Geo.savePoint(points[0])
        .then(function() {
          console.log('geo point has been stored');
        })
    })
}  

Regards, Vlad

Thanks you,
Working now:)
Can the condition be set to multiple objectID as well?

Can the condition be set to multiple objectID as well?

yes, of course, you can use any valid whereClause.