Skip to content

How can I access & sterilize my Javascript user object from Parse server before sending to client?

I do not have a clue why I cannot sterilize my user object array server side. I am having issues accessing any properties at all server side no matter what I’ve tried I get either errors or empty results. In my Flutter/Dart code I can access any properties as normal but of course I do not want users to be able to get all such data.

So the question is, why can’t I access properties server side and how can I do this?

EDIT: On some help in comments it has been discovered that this “object” is not in fact an array, this makes things more difficult. Also does not make sense that I can access the “not array” normally in my flutter dart code… any ideas whats wrong?

For below I am following this My code that works, returning a list of Parse User objects:

Parse.Cloud.define("ccRoleUsersQuery", async function(request) {
    const query = await new Parse.Query(Parse.Role).equalTo('users', request.user).find({ useMasterKey: true })
    let users = query[0].getUsers().query().find({ useMasterKey: true });
    let steralizedResults = [];
    for (let i = 0; i < users.length; i++) {
        let name = users[i].get("name");         // Does not access object
      let publicKey = users[i]["publicKey"];     // Does not access object
      steralizedResults.push(name);          
      steralizedResults.push(publicKey);    
    }
    return users;            // steralizedResults if used returns empty array
  });

Users array returned as above:

[{username: user003, email: [email protected], createdAt: 2021-09-26T23:37:37.594Z,
updatedAt: 2021-10-05T01:25:43.989Z, publicKey: 445trTREttY654FFFGgt5ydfsg, name: 003, ACL: {6m9LPbxD8V: {read: true, write: true}}, objectId: 6m9LPbxD8V, __type: Object, className: _User}, 
{username: user004, createdAt: 2021-10-03T22:19:27.754Z, updatedAt: 2021-10-06T23:24:07.576Z, email: [email protected], publicKey: GTRg554gtr8yvfdsv43fdsv334, name:
004, ACL: {t9joISsGwO: {read: true, write: true}}, objectId: t9joISsGwO, __type: Object, className: _User}]

Answer

‘Tis the season to be async:

query.find({
  success: function(results) {
    // results is an array of Parse.Object.
  },
  error: function(error) {
    // error is an instance of Parse.Error.
  }
});

Also special functions you can use on the Parse.Query object: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/parse/index.d.ts (search for class Query)