Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse server throws an error when saving an object with a pointer field set to undefined (Postgres only) #9146

Open
andrewalc opened this issue Jun 6, 2024 · 5 comments · May be fixed by #9147
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@andrewalc
Copy link

New Issue Checklist

Issue Description

For Parse server connected to a postgres db, if an object has a Pointer field and that field is set to undefined and saved Parse server will error with TypeError: Cannot read properties of undefined (reading 'objectId')

The full error which I've put below leads to this line in the parse server code

valuesArray.push(object[fieldName].objectId);

some additional context to the code

 switch (schema.fields[fieldName].type) {
    case 'Date':
      if (object[fieldName]) {
        valuesArray.push(object[fieldName].iso);
      } else {
        valuesArray.push(null);
      }
      break;
    case 'Pointer':
      valuesArray.push(object[fieldName].objectId);
      break;
    case 'Array':
      if (['_rperm', '_wperm'].indexOf(fieldName) >= 0) {
        valuesArray.push(object[fieldName]);
      } else {
        valuesArray.push(JSON.stringify(object[fieldName]));
      }
      break;

It appears that maybe object[fieldName]) needs an if check as it does in the Date case?

Also looking further up

if (object[fieldName] === null) {

It looks like this createObject function in the Postgres adapter does check and skip values that are null. Switching the value to null instead of undefined does save without error.

// This works fine
const test2 = new Parse.Object("SomeObject");
test2.set("pointerTo", null);
await test2.save(null, { useMasterKey: true });

Steps to reproduce

// Start a blank Parse Server that is connected to a Postgresql database
// Run the following code on the server after start up

// Create a parse object class
const pointerTestObj = new Parse.Object("PointerTest");
await pointerTestObj.save(null, { useMasterKey: true });

// Create another class that has a pointer to the above class
const obj = new Parse.Object("SomeObject");
obj.set("pointerTo", pointerTestObj);
await obj.save(null, { useMasterKey: true });

// Create another object, but this time the field is set to undefined
const test = new Parse.Object("SomeObject");
test.set("pointerTo", undefined);
await test.save(null, { useMasterKey: true }); // Throws an error on save

Actual Outcome

Saving the object with an undefined pointer field results in:

/src/node_modules/parse-server/lib/ParseServer.js:265
          throw err;
          ^

TypeError: Cannot read properties of undefined (reading 'objectId')
    at /src/node_modules/parse-server/lib/Adapters/Storage/Postgres/PostgresStorageAdapter.js:1220:46
    at Array.forEach (<anonymous>)
    at PostgresStorageAdapter.createObject (/src/node_modules/parse-server/lib/Adapters/Storage/Postgres/PostgresStorageAdapter.js:1173:25)
    at /src/node_modules/parse-server/lib/Controllers/DatabaseController.js:684:29
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected Outcome

The object should be able to set a pointer to undefined and save without throwing as it does when connected to a mongo database

Environment

Server

  • Parse Server version: 6.5.6
  • Operating system: macOS Somona 14.3
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): Local

Database

  • System (MongoDB or Postgres): Postgres
  • Database version: postgres:16.3-alpine
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): Local

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): N/A
  • SDK version: N/A

Logs

Copy link

parse-github-assistant bot commented Jun 6, 2024

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

@andrewalc
Copy link
Author

made a pr to try and make a failing test case. is there a proper way to run the tests with postgres locally? i see mongodb-runner runs when i try npm run test as the new contributor guide suggests but i couldn't find how to do it with postgres.

@cbaker6
Copy link
Contributor

cbaker6 commented Jun 7, 2024

is there a proper way to run the tests with postgres locally? i see mongodb-runner runs when i try npm run test as the new contributor guide suggests but i couldn't find how to do it with postgres.

Install Postgres via docker: https://github.com/parse-community/parse-server/blob/alpha/CONTRIBUTING.md#postgres-with-docker

Run tests: https://github.com/parse-community/parse-server/blob/alpha/CONTRIBUTING.md#test-against-postgres

@mtrezza mtrezza added the type:bug Impaired feature or lacking behavior that is likely assumed label Jun 7, 2024
@andrewalc
Copy link
Author

ah i completely missed this, gonna try this out thank you!

is there a proper way to run the tests with postgres locally? i see mongodb-runner runs when i try npm run test as the new contributor guide suggests but i couldn't find how to do it with postgres.

Install Postgres via docker: https://github.com/parse-community/parse-server/blob/alpha/CONTRIBUTING.md#postgres-with-docker

Run tests: https://github.com/parse-community/parse-server/blob/alpha/CONTRIBUTING.md#test-against-postgres

@andrewalc
Copy link
Author

andrewalc commented Jun 11, 2024

Sorry haven't had much time to get back to this. I got the postgres tests working locally but I can't seem to reproduce my issue in a failing test. I was at least able to try and make a repo with a branch where i'm encountering the issue. I thought I may have been using the wrong postgres image postgres:16.3-alpine but I switched it to postgis/postgis:latest and i still run into the error. im using docker compose here https://github.com/andrewalc/parse-min-repro/tree/test/parse-postgres-bug

sorry closed the issue by accident

@andrewalc andrewalc reopened this Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
3 participants