Set attributes for results on Parse Server


#1

I am using Parse Server for an Ionic3/Angular 4 Application.

I perform a query on Cloud Code on Parse Server and I retrieve the results of two Pointer columns.

Table Structure

User ->username

Friends -> toUser(Pointer) -> fromUser (Pointer)

Cloud Code

Parse.Cloud.define('FriendsQuery', function(req,res) {
    const query1 = new Parse.Query("Friends");  
    const query2 = new Parse.Query("Friends"); 
    query1.equalTo("fromUser", { "__type": "Pointer", "className": "_User", "objectId":  req.params.currentuser });
    query2.equalTo("toUser", { "__type": "Pointer", "className": "_User", "objectId":  req.params.currentuser });
    const friendquery = Parse.Query.or(query1, query2);
    friendquery.find().then((results) => {
    res.success(results);
})
.catch(() =>  {
  response.error("user lookup failed");
});
});

Function

retrieveFriends(){
   if (this.currentUser= Parse.User.current()) {
     console.log(this.currentUser.id)
     Parse.Cloud.run('FriendsQuery',{currentuser: this.currentUser.id}).then(
     res => {       
       this.tmp2 = res;
       console.log(this.tmp2);
       this.localdata.setFriends(this.tmp2);
       this.friends = this.localdata.tmpfriends;
       console.log(this.friends);
       });

   }
   }

Html Code

<ion-item class="item-avatar item-avatar-left item-button-right common-list" *ngFor="let friend of friends">
    <ion-avatar *ngIf="friend.fromUser.objectId == currentUser.id" item-left><img style="left:0px !important;margin-top:0px !important;" [src]="friend.toUser.avatar  || '/assets/img/no-avatar.png'"/></ion-avatar>
    <ion-label *ngIf="friend.fromUser.objectId == currentUser.id">{{friend.toUser.username}}</ion-label>
    <ion-avatar *ngIf="friend.toUser.objectId == currentUser.id" item-left><img style="left:0px !important;margin-top:0px !important;" [src]="friend.fromUser.avatar  || '/assets/img/no-avatar.png'"/></ion-avatar>
    <ion-label *ngIf="friend.toUser.objectId == currentUser.id">{{friend.fromUser.username}}</ion-label>                
</ion-item>

As you can see in hmtl code I have to check the current logged in user to display the right results, his friends.

This is not very wise and this check should be done differently, either on Parse either on a function in order to set an attribute like this:

friend.toUser = something friend.fromUser = something

in order to display in html code something.username to retrieve the friends.

Should I make the check on Cloud Code or locally on a function?

Thank you