parse-community / parse-community/parse-server

object.relation(..) throws "Invalid key '...'" when beforeFind() trigger change the query

Open
#7,269 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
21.4k
Forks
4.8k
Avg merge
7h 45m
Merged PRs (30d)
11

Description

New Issue Checklist
Issue Description

Hi !

I have a beforeFind trigger that add a few restrictions on a child type, when executing parent.query().find() the server throw a Invalid key "$relatedTo"

Steps to reproduce

I have a Parent with a relation to a Child

Then:

// /cloud/index.js
  Parse.Cloud.beforeFind("Child", (req) => {
      const a = new Parse.Query("Child")
      const b = new Parse.Query("Child")

      a.equalTo('field', 'abcd')
      b.doesNotExist('field')

      return Parse.Query.and(req.query, Parse.Query.or(a, b)  )
})   

On the client

// client
const parent = new Parent()

await parent.relation('childs').query().find()
Actual Outcome
Error in mounted hook (Promise/async): "ParseError: 105 Invalid key name: $relatedTo"
Expected Outcome

An happy relation! 👪

Failing Test Case / Pull Request

It seems that reduceRelationKeys only checks for a Top level $or, and in my case, the top level operator is $and

This seems like it 'fixes' the issue :

  reduceRelationKeys(className, query, queryOptions) {
//    if (query['$or']) {
//      return Promise.all(query['$or'].map(aQuery => {
//        return this.reduceRelationKeys(className, aQuery, queryOptions);
//      }));
//    }
    for(const op of ['$or', '$and' , '$not']) {
      if (query[op]) {
        return Promise.all(query[op].map(aQuery => {
          return this.reduceRelationKeys(className, aQuery, queryOptions);
        }));
      }
    }
// ....
  • 🤩 I submitted a PR with a fix and a test case.
  • 🧐 I submitted a PR with a failing test case.
Environment

Server

  • Parse Server version: 4.5.0
  • Operating system: windows
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): local

Database

  • System (MongoDB or Postgres): mongo
  • Database version: 4.4?
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): local

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): js
  • SDK version: 3.1.0
Logs

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in /cloud/index.js and the client relation query, then inspect reduceRelationKeys where nested $or, $and, and $not operators are handled. Reproduce the beforeFind query against the documented MongoDB setup and verify that parent.relation('childs').query().find() completes without the Invalid key name: $relatedTo error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.