Validation failed on nested cloud code functions with requireUser: true

I get this error

error: Failed running cloud function testGetAllUsers for user undefined with:
  Input: {}
  Error: {"message":"Validation failed. Please login to continue.","code":142} {"error":{"code":142,"message":"Validation failed. Please login to continue."},"functionName":"testGetAllUsers","params":{}}

when I try to run this code:

Parse.Cloud.define("testValidation", async (request) => {
   
   const result = await Parse.Cloud.run("testGetAllUsers", {});
   return ressult
   
});

where testGetAllUsers is

Parse.Cloud.define("testGetAllUsers", async (request) => {
      
   const { params, user } = request
   
   // Create User class instance
   const userClass = Parse.Object.extend(Parse.User);

   try {
      const userQuery = new Parse.Query(userClass);
      userQuery.includeAll()
      
      return await userQuery.find({ useMasterKey: true });
   } catch(error) {
      throw error;
   }

}, {
   requireUser: true
});

So basically, when I run testGetAllUsers itself, everything is fine. It sees the User who’s logged in. However, whenever I call one function who has requireUser: true from another function, I get the error mentioned above.

Is there way to pass the user to the nested function?

Parse.Cloud.define("testValidation", async ({user}) => {
   const result = await Parse.Cloud.run("testGetAllUsers", {}, { sessionToken: user.getSessionToken() });
   return result;
}, {
  requireUser: true
});

Running return await userQuery.find({ useMasterKey: true }); is problematic though as all users’ private data will be returned, including email, sessionToken, hashed_password, etc. I would strip out the data using exclude, select, or mutating the objects before passing to the client.

1 Like

sorry, this was just a test function and not really something I would do for production.

the main problem was just about calling nested cloud code functions and getting the said error.

that said, Thanks! that worked.

I didn’t / couldn’t find any info on that 3rd parameter that .run() could receive.

Edit: for future reference of the options parameter:

@dblythy thanks for this again, but if I may ask while we’re on the topic of .run options,

 if (options.context && typeof options.context === 'object') {
    requestOptions.context = options.context;
  }

what would the options.context be used for?

https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Cloud.html#.run

context gets passed to all cloud triggers, and can be very helpful to provide additional information to your trigger. You can pass context in .save, .find calls, etc, and can access via req.context.

Here is the example in the JS docs:

const TeamMember = Parse.Object.extend("TeamMember");
const teamMember = new TeamMember();
teamMember.set("team", "A");

const context = { notifyTeam: false };
await teamMember.save(null, { context: context });

The context is then accessible in Cloud Code:

Parse.Cloud.afterSave("TeamMember", async (req) => {
 const notifyTeam = req.context.notifyTeam;
 if (notifyTeam) {
   // Notify team about new member.
 }
});

I see that you opened a SO issue a few years ago relating to context. I’ve opened an issue in the docs repo to improve this. If you have any other recommendations for improvements to our docs (or would like to be involved in the doc improvement process), please let us know :blush:

1 Like

haha yeah. I didn’t get a reply too. thankfully this forum opened up.

I still don’t know the answer to that question I had on SO. I still don’t know why beforeSave was assigned to a const based on the current documentation.

or would like to be involved in the doc improvement process

Oh you opened a can of worms on that one.

I’d love to. and I had updated docs before where I found outdated information etc. Unfortunately, I can’t help on what I don’t understand. lol

As per suggestion on how to improve the documentation I have these off of the top of my head.

  • 1st: I would suggest is placing the parse-server version number the documentation is for so it’s clear that the documentation we’re reading is for the that version (latest or otherwise) and we know that everything inside it is updated and current. With Parse Documentation, I can never tell if the documentation is current or not. I’ve ran into a few outdated examples in the past personally where I did update the docs and did pull requests. If we needed documentation for Parse-server version 3 and it’s matching api’s, where do we find it? I don’t see versioning of the docs here on docs.parseplatform.org.

  • 2nd: Have step somewhere in the process where you can get a group of noob volunteer Parse programmers to run through the docs (especially updates) to just have a wider “quality check” process to include the people who would actually use the docs to find information on how to do this or that with Parse. To see if it’s clear. You have to break it down to basics as these docs for its intended audience.

For example: I’ve been spending quite a bit of my time the past month to setup my own Parse-Server on heroku + mongodb + amazon s3. All of which I have no experience on. NodeJs, Express, mongodb, amazon S3 - nada. I had to learn all of that. The current documentation on setting up a parse server I believe can be a lot better. Setting up the S3Adapter alone was quite confusing.

“Now head to the Identity and Access Management (IAM) service.”

Where is that??? I don’t know. I had to search for it.

  • 3rd. SDK / API reference has so many functions I don’t know how to use or where to use them. Those each need examples themselves. Only some have examples. Other more advanced functions don’t have examples.

Long story short, after typing all that, I realized that getting a quality check team for the docs set up would be a good way to go, where “stupid questions” won’t be ridiculed. I think that will bring into light quite a bit of things we can fix. Is there a documentations team already? If there is, I don’t know about it. Maybe have that also spotlighted either on the Parse website or in the community forums where people can join and offer help.

This reply is getting long. Hopefully I didn’t overstep.

thanks for listening. :smiley:

So getting back to context - I think I get it after seeing the example. So it’s basically additional information we want to pass to the cloudcode function outside of the object we’re sending. got it!

Knowing that, and knowing that options in .run(name, data, options) is actually context now makes sense to me. (another reason to have examples in the SDK reference.

thanks again.

The current version of the docs is always in the docs url, such as https://parseplatform.org/Parse-SDK-JS/api/3.4.2/Parse.ACL.html#getWriteAccess (note the 3.4.2).

If you’re after the latest version, https://parseplatform.org/Parse-SDK-JS/api will always point to the latest.

The docs repo is huge, this would be a massive undertaking. If anyone here would like to volunteer to go through the repo then feel free to jump in and break it down into smaller PRs.

The SDK / API reference is autogenerated, ideally all the functions in that are covered by our docs repo.

Perhaps a good place to start would be adding information in the docs for how to run a cloud function from another cloud function as a user, somewhere on this page.

There are no stupid questions, we are a community and all here to help.

Documentation is a pretty easy but time consuming task, and at the moment most of our time goes into the fun stuff like new features, security updates, etc. Docs are a good way to get into contributing without needing to have much technical knowledge of the project, so we welcome newer developers who are interested in potentially becoming contributors to have a crack at amending the docs.

1 Like