I'm using the latest version of Parse Server, with Typescript compilation before shipping the .js files. Mind that this whole question is specific to Cloud Code, and not client implementations.
This has been working quite well this far, but now I found a problem which I seriously have no idea how to solve. Consider a class for handling email events, which has a method for finding a user's email for a given ID, and sending him a message:
async sendEmailToUserID(userId: string, subject: string, text: string){
const res = await new Parse.Query("User").equalTo("objectId", userId).find();
if(res){
const user = res[0];
const email = user.get("email");
return await this.sendEmail(email, subject, text);
}
}
I think the function is quite straightforward and easily explains itself. Well, thing is, that the find() function can properly retrieve the user's object, but the .get("email") thing won't ever work: it always returns "undefined".
Anyone's got an idea on why this happens?
PD: yes, the "email" fields exists for any given user.
Okay, took a while to realise how to find it since Parse doesn't have any errors dedicated to this. The problem is that I was trying to access a User class object, which is protected, without using the master key.
Mind that the following solution is intended for Cloud Code only; it's definitely not safe when used from the client code:
Just add {useMasterKey: true} to the .find() method and it will work:
const res = await new Parse.Query("User").equalTo("objectId", userId).find({useMasterKey: true});