medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.58k stars 2.42k forks source link

CustomerService.retrieve requests fail with error "Cannot read properties of undefined (reading 'joinColumns') #6103

Open ccarollo opened 8 months ago

ccarollo commented 8 months ago

Bug report

After installing a base version of medusa, and then making a number of additions within the src/ directory the CustomerService begins to fail with the following error...

error:   Cannot read properties of undefined (reading 'joinColumns')
TypeError: Cannot read properties of undefined (reading 'joinColumns')
    at /Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/query-builder/SelectQueryBuilder.js:1172:38
    at Array.map (<anonymous>)
    at SelectQueryBuilder.createJoinExpression (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/query-builder/SelectQueryBuilder.js:1118:57)
    at SelectQueryBuilder.getQuery (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/query-builder/SelectQueryBuilder.js:54:21)
    at SelectQueryBuilder.executeEntitiesAndRawResults (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/query-builder/SelectQueryBuilder.js:1985:18)
    at SelectQueryBuilder.getRawAndEntities (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/query-builder/SelectQueryBuilder.js:684:40)
    at SelectQueryBuilder.getOne (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/query-builder/SelectQueryBuilder.js:711:36)
    at EntityManager.findOne (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/entity-manager/EntityManager.js:609:14)
    at ChildClass.findOne (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/typeorm/repository/Repository.js:255:29)
    at CustomerService.<anonymous> (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/@medusajs/medusa/dist/services/customer.js:266:59)
    at step (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/@medusajs/medusa/dist/services/customer.js:59:23)
    at Object.next (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/@medusajs/medusa/dist/services/customer.js:40:53)
    at /Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/@medusajs/medusa/dist/services/customer.js:34:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/@medusajs/medusa/dist/services/customer.js:30:12)
    at CustomerService.retrieve_ (/Users/christian/Documents/Development/repos/github/solocrete-supply/node_modules/@medusajs/medusa/dist/services/customer.js:259:16)

System information

Medusa version (including plugins): 1.18.1 Node.js version: v20.9.0 Database: PostgreSQL 16.0 Operating system: Mac OS 14.2.1 Browser (if relevant): Chrome 120.0.6099.216

Dependencies List

dependencies
    "@medusajs/admin": "7.1.8",
    "@medusajs/cache-inmemory": "^1.8.9",
    "@medusajs/cache-redis": "^1.8.9",
    "@medusajs/event-bus-local": "^1.9.7",
    "@medusajs/event-bus-redis": "^1.8.10",
    "@medusajs/file-local": "^1.0.2",
    "@medusajs/inventory": "^1.11.4",
    "@medusajs/medusa": "^1.18.1",
    "@medusajs/stock-location": "^1.11.3",
    "@tanstack/react-query": "4.22.0",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "16.3.1",
    "express": "^4.17.2",
    "medusa-fulfillment-manual": "^1.1.38",
    "medusa-interfaces": "^1.3.7",
    "medusa-payment-manual": "^1.0.24",
    "medusa-payment-stripe": "^6.0.6",
    "medusa-plugin-algolia": "^0.2.20",
    "medusa-plugin-contentful": "^1.2.11",
    "medusa-plugin-ip-lookup": "^1.2.8",
    "medusa-plugin-restock-notification": "^7.0.3",
    "medusa-plugin-segment": "^1.3.7",
    "prism-react-renderer": "^2.0.4",
    "taxjar": "^4.1.0",
    "typeorm": "^0.3.16"

devDependencies
  "@babel/cli": "^7.14.3",
  "@babel/core": "^7.14.3",
  "@babel/preset-typescript": "^7.21.4",
  "@medusajs/medusa-cli": "^1.3.21",
  "@types/express": "^4.17.13",
  "@types/jest": "^27.4.0",
  "@types/node": "^17.0.8",
  "babel-preset-medusa-package": "^1.1.19",
  "contentful-migration": "^4.18.2",
  "cross-env": "^7.0.3",
  "eslint": "^6.8.0",
  "jest": "^27.3.1",
  "rimraf": "^3.0.2",
  "ts-jest": "^27.0.7",
  "ts-loader": "^9.2.6",
  "typescript": "^4.5.2"

Steps to reproduce the behavior

  1. Install version 1.18.1 of medusa backend
  2. npm install
  3. Copy an .env file into the medusa root
  4. Edit the package.json of the @medusajs/admin, change the main key to be the following "main": "lib/index.js"
  5. Run npx medusa develop, the application should work
  6. Replace the src in the medusa backend with the src folder that is attached
  7. Stop medusa, restart medusa
  8. Then test of one the newly installed admin api routes using the following curl command curl -X GET http://localhost:9000/admin/paymentterms. It shouldnt work. With the error unauthorized. However the export api route has been setup to not require authentication. If you now go into the
  9. But if you go into the route.ts file at /src/api/admin/paymentterms and resave it (no edits necessary) and then try the curl command again it works.
  10. Lastly if you go into a customer detail record in the admin console you should now see a react error that looks like the screenshot below and if you check the medusa terminal console you will see an error like the one above.

Expected behavior

A clear and concise description of what you expected to happen

I should be able to access the /admin/customers/ without it returning an error and a 500. This error also appears to affect the /store/auth for the storefront.

Screenshots

Screenshot 2024-01-16 at 2 57 05 PM Screenshot 2024-01-16 at 2 57 36 PM Screenshot 2024-01-16 at 2 58 02 PM

Code snippets

src.2.0.zip

olivermrbl commented 8 months ago

@adrien2p, could you investigate this issue?

adrien2p commented 8 months ago

I ll have a look at this one

adrien2p commented 8 months ago

@ccarollo from what I can see, you have created and Address entity which does not have a relation to the customer, which is what is expected by medusa. Therefore, typeorm does not find the inverse relation between the customer and the address.

ccarollo commented 8 months ago

@adrien2p thank you for explaining. Two questions...

  1. Is there an example of the proper way to create an Address entity that has the Customer relation correctly defined?
  2. Is there a schema diagram (ERD) or document that clearly defines the relations between entities so that this kind of issue can be avoided in the future?
adrien2p commented 8 months ago

@shahednasser do we have this in our resources? the entities and there relations.

@ccarollo if you would like to add more properties to the already existing Address entity, we have a documentation on how to extend a core entity

shahednasser commented 8 months ago

We have a guide here on how to extend an entities: https://docs.medusajs.com/development/entities/extend-entity

We also have an entity reference where you can browse all entities and see their properties, including their relations: https://docs.medusajs.com/references/entities/classes/Address

In the reference, you'll find that relations have a blue icon next to them indicating that they can be expanded. You can hover over the icon for more details.

srindom commented 7 months ago

@ccarollo - can you maybe explain why you added the Address entity?