dear all,
I trying to get a table called dep that has a relation in field called codes.
there are no more then 100 codes per dep, but the limit is 10.
I tried all I know, but I missing something.
how can I do that.
here is the code I tried, but because of promise, I can not achieve it.
for (var counter=0;counter<systemDepTypes.length;counter+=1)
{
var codeBuilder = Backendless.DataQueryBuilder.create();
//query for each item…
var myCodes=[];
//dep=‘systemDepTypes[counter].name’
var whereClause=“dep=’”+systemDepTypes[counter].name+"’"
codeBuilder.setPageSize( 100 ).setOffset( 0 );
codeBuilder.setWhereClause( whereClause );
Backendless.Data.of( $rootScope.Classes.codes).find(codeBuilder)
.then( function( result ) {
for (var codeCount=0;codeCount<result.length;codeCount+=1)
{
myCodes.push(result[codeCount]);
}
//systemDepTypes[counter].codes=myCodes;
gotCode.push(myCodes);
})
.catch( function( error ) {
console.log(“oops:”+error);
})
//console.log(systemDepTypes[counter].name+" codes:"+myCodes.length);
}
please help
You should try two-step retrieval (see here how to do it: https://backendless.com/docs/js/doc.html#two-step-retrieval)
In two words, first you retrieve parent object and then you take it’s objectId
and retrieve it’s relations using method
Backendless.Data.of(TableName).loadRelations(parentObjectId, loadRelationsQueryBuilder)
will give it a try again, last time, not worked correctly.
still I have problem with findSync, it’s not loading all the info
Sync method should not load all the relations. They are limited to 10 in that way you are loading them. But LoadRelations API gives you the possibility to load any quantity of related objects (still don’t forget that one page may contain no more that 100 objects).
tried, not working
here is the code:
for (var counter=0;counter<systemDepTypes.length;counter+=1)
{
var parentObjectId = systemDepTypes[counter].objectId;
var loadRelationQueryBuilder = Backendless.LoadRelationQueryBuilder.create();
loadRelationQueryBuilder.setRelationName(“codes”);
loadRelationQueryBuilder.setPageSize(100);
Backendless.Data.of($rootScope.Classes.dep).loadRelations(parentObjectId,loadRelationQueryBuilder);
console.log(systemDepTypes[counter].name+":"+systemDepTypes[counter].codes.length);
}
Each call to server returns a Promise object that you should handle properly.
Here you are making a call to server and without waiting to response trying to get some values. Moreover, loadRelations
does not write result into parent object. It’s just return an array of related objects.
So here you should do something like this:
...
Backendless.Data.of($rootScope.Classes.dep).loadRelations(parentObjectId, loadRelationQueryBuilder)
.then(codes => { // do something with your codes. Here you have parentObject id and it's codes array})
I understand that, but since I using .then, the result can not update my global variable which need to hold all the info inside