'afterFind()' Event -> Add relational fields to data

When I find a table (let’s call it Lightsabers), how do I add a relational column to LightSabers during afterFind() that the default SDK will translate/parse?
For example for iOS SDK:
Lightsabers object

class Lightsaber: NSObject {
var color: String?
var strength: Strength? -> this is the relational field i'm adding in the server code
}

Relational Strength

class Strength: NSObject {
var power: Int = 0
var super: String?
}

Find Lightsabers function

let dataStore = Backendless.sharedInstance().data.of(Lightsaber.ofClass())



func findSync(){
var error: Fault?

let result = dataStore?.find(&error)
if error == nil {
let lightsabers = result?.getCurrentPage()
for obj in lightsabers! {
print("lightsabers strength: \(obj.strength)") // It parses correctly
}
}
else {
print("Server reported an error: \(error)")
}

Hi Brian,

Would you be more comfortable with JS or Java to implement that functionality?

Regards,
Mark

Mark,

JS

–Brian

No problem. Have you tried creating an afterFind event handler in JS? Look at the generated comments for the code. One of them is for the object which will be returned back to the client (sorry, I am not in front of a computer right now and cannot show an example). So the object which will be returned is the one you need to modify in the event handler.

Hope this helps.

Regards,
Mark

My fault… What I meant to post was a relational column NOT simple data column. I’ve edited my original post.

To answer your question, yes I have created an afterFind event handler in JS. The problem I’m having is adding the correct keys so the default SDK knows how to parse the relational object.

Hi, Brian.
Please, could you show your code from afterFind() where you try add the related object.

Oleg,

Sure

Backendless.ServerCode.Persistence.afterFind('Person', function(req, res) { 
 for (var i = 0; i < res.result.data.length; i++) { 
 res.result.data.strength = {"___class": "Strength", "power": 56, "super": "teleport"}; // What are the keys that should be added? }});
}
});


Hi Brian,

Adding a related object would look like this:

Backendless.ServerCode.Persistence.afterFind('YOUR-TABLE-NAME', function(req, res) {
var objToAdd = {
___class = "RelatedTableName",
propName1 = "value1",
propName2 = "value2"
}
res.result.data.forEach( item => {

  item.CUSTOMPROP = objToAdd;
});

This will add the “CUSTOMPROP” property for every object in the resulting collection. Please keep in mind that a simpler way would be to enable “auto-load” for the related collection in Backendless Console.

Please let me know if it works for you.

Regards,
Mark

I try your code. Your have a little mistake.
Here:

res.result.data[ i ].strength = {"___class": "Strength", "power": 56, "super": "teleport"};

But pay attention, that new Strength object will be transient. the caller will receive modified object, but it won’t be saved in database.

Thank you both, Oleg and Mark. You guys are fast and way better than your competitors.

My code was correct (minus the syntax errors). It was a dev error on the application side.

Just for reiteration and clarification:
“___class” key tells the parser which Class to map

Yes, you are right.

Thank you.
Feel free to ask us, if you faced with troubles.