Backendless Support
 
Solved

Database response "objectref" fields

Hello Guys,

I noticed with the growing size of my database responses, the number of "___objectref" fields in the json structure rises. These are always there instead of loaded relations. It seems this field replaces a related object which was already previously in the return string from the database.

In my mail you can find a response from my API fetched over the Backendless Console which gives this command:

curl

-X "GET" "https://api.backendless.com/63087FF7-7E1D-973D-FF3B-D5F0740FA100/5C363F28-6536-9660-FFAF-2B85F9EDFB00/services/EightCloudService/acquisition/metadata?ID=-1&Offset=0&Shareddata=false&Deleted=false"

\

-H 'Content-Type: application/json' \

-H 'Accept: application/json' \

-H 'user-token: 531BC87D-C621-7D37-FF64-D6DE596B9F00'

I hope you can fix this or give me a hint how I can avoid this, as this stops my logic and GUI from working properly, as there are obviously data missing in the json tree where the program expects the omitted objects.

Thanks and Best regards

Best Answer
photo

Hey Jonas

Please take a look at this utility module which is able to transform __objectref fields back into referred objects

Just integrate that module into your project and use its parse function instead of JSON.parse

Leave a Comment

Comments (12)

photo
1

Hi Jonas,

Are there any cyclic references in the response object? If there are, there is no other way to return such an object than to replace them with references.

Also, do you have any JS event handlers which interfere this exact request?

photo
1

Hi Sergey,

yes, there is a custom JS cloud code handler. This checks some permissions of the user and then reads the data from the database and returns them without modifying any data.

No, there are no cyclic references. This is the sceme of the actual relations:

  • CrtData: Objects in an extra table with more relations
  • User: is the entry in the Users table
  • UserVariables: Additional data in extra table without any further relations

  1. --------------------Scan-------------
  2. | _______________ | __________________ |

    CrtData _______ UserVariables _____ User

  3. ---------CrtData--------
  4. | _______________________ |

    Company _____________ User

  5. Poorly the forum ignores preformatted text, so ignore the underscores

The user relation from Scan and CrtData points in most cases to the same user, but can differ.

It seems that as soon as a user object once occured in the data, all other relations are replaced by an objectref field.

Attached is a file with the data structure returned by the database. Hope this clarifies things a bit

Cheers,

Jonas

photo
1

Any updates on this problem? Poorly it's a complete show stopper for our application when the corresponding data for the relations are not in the json response...

photo
1

In the meantime, I tried the Data Rest API. There it is the same behaviour, objects which appear multiple times in the data, are replaced by the tag "__originSubID: <GUID>" and the first occurence of this object gets an additional field: "__subID: <GUID>".

In the data returned by the Cloud JS code the object is, as mentioned in my first post, replaced by the tag "___objectref: <ID>", but the first occurence of the object does not get any new identifier.

photo
1

I tested now a bit further the difference between the REST API and the Cloud JS code in my handler.

This is the curl command I use to get data from the

  1. curl -X GET -k -H 'user-token: <token>' -i 'https://api.backendless.com/<app>/<key>/data/<tablename>?loadRelations=CrtData,User,UserVariables&relationsDepth=2'

With that command I get the above mentioned "__originSubID" in the first occurence and the "__subID" fields instead of the original object

To test the cloud JS code, I created an extra handler in my app:

  1. /**
  2. * @route GET /testdataread
  3. * @returns {Object}
  4. */
  5. async testdataread()
  6. {
  7. var RelationsList = ['User', 'CrtData', 'UserVariables'];
  8. var WhereClause = 'CompanyId = 4 and Deleted = false';
  9. return (await Backendless.Persistence.of('eightcloud_AcquisitionMetaData').find(Backendless.DataQueryBuilder.create().setWhereClause("").setRelated(RelationsList).setRelationsDepth(2).setSortBy(["ID DESC"]).setPageSize(100).setOffset(0)))
  10. }

This gives me the same data, but instead of the original related object, I only get the "___objectref: <ID>" field. And the first object occurence of the related does not get any additional reference field to identify the object as the ID of the "___objectref: <ID>" replacments.

photo
1

Hey Jonas

Please take a look at this utility module which is able to transform __objectref fields back into referred objects

Just integrate that module into your project and use its parse function instead of JSON.parse

photo
1

Hi Vitaly,

how can I integrate that? With npm? Or just copy the code?

And what shall I pass for the "classMappings" parameter of the parse function?

Thanks

photo
1

It is available for you if you are using the js coderunner or you can just copy the code

photo
1

Just copy the code. Don't pass second argument

photo
1

Very nice, thank you both.

I copied the file to my application code and do:

  1. var BackendlessUtilJson = require('./Backendless.Util.Json.js');
  2. DbData = BackendlessUtilJson.parse(JSON.stringify(DbData));

This resolvs all the __objectref fields :)

Sergey, how should it be available when I use coderunner? The code I do this in, is my business logic code of my application. So I guess it runs in the coderunner. Would be more elegant to use the Backendless lib then.

photo
1

Sorry,

I've made mistake it is not available fro coderunner, you have to copy this code

photo
1

Ok, alright. No problem.

Then you can mark this topic as solved.