medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.15k stars 2.65k forks source link

[Bug]: Cannot read properties of undefined (reading 'getFreshManager') #10214

Closed DonielMoins closed 1 day ago

DonielMoins commented 5 days ago

Package.json file

{
  "name": "medusa-eats-backend",
  "version": "0.0.1",
  "description": "A Medusa backend for Medusa Eats.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa",
    "medusa-eats"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "setup-db": "npx medusa build && npx medusa db:migrate && npx medusa db:sync-links  && medusa exec ./src/scripts/seed.ts && npx medusa user -e daniel@moins.tech -p supersecret"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.0.2",
    "@medusajs/cli": "2.0.2",
    "@medusajs/framework": "2.0.2",
    "@medusajs/js-sdk": "^2.0.2",
    "@medusajs/medusa": "2.0.2",
    "@medusajs/types": "^2.0.2",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "@tanstack/react-query": "^5.59.20",
    "awilix": "^8.0.1",
    "cors": "^2.8.5",
    "dotenv": "16.3.1",
    "express": "^4.17.2",
    "jsonwebtoken": "^9.0.2",
    "lucide-react": "^0.379.0",
    "pg": "^8.13.0",
    "prism-react-renderer": "^2.0.4",
    "prop-types": "^15.8.1",
    "uber-direct-sdk-moinstech": "^1.6.19",
    "uuid": "^11.0.2"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.0.0",
    "@mikro-orm/cli": "5.9.7",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "@stdlib/number-float64-base-normalize": "0.0.8",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/express": "^4.17.13",
    "@types/jest": "^29.5.13",
    "@types/jsonwebtoken": "^9.0.6",
    "@types/mime": "1.3.5",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "@types/uuid": "^10.0.0",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v22.9.0

Database and its version

PostgreSQL 16.4 (Debian 16.4-1.pgdg120+1)

Operating system name and version

6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Browser name

No response

What happended?

When trying to access the service's objects using the query tool I am getting an error related to getting a fresh instance to the manager

Expected behavior

Return a list of all the service's objects that I am requesting

Actual behavior

` error: Cannot read properties of undefined (reading 'getFreshManager')

TypeError: Cannot read properties of undefined (reading 'getFreshManager') at UberDeliveryService.descriptor.value [as listAndCountUberDeliveries] (/home/donielmoins/medusa-eats/backend/node_modules/@medusajs/utils/src/modules-sdk/decorators/inject-manager.ts:44:56) at RemoteQuery.remoteFetchData (/home/donielmoins/medusa-eats/backend/node_modules/@medusajs/modules-sdk/src/remote-query/remote-query.ts:257:41) at RemoteJoiner.fetchData (/home/donielmoins/medusa-eats/backend/node_modules/@medusajs/orchestration/src/joiner/remote-joiner.ts:477:33) at RemoteJoiner.query (/home/donielmoins/medusa-eats/backend/node_modules/@medusajs/orchestration/src/joiner/remote-joiner.ts:1230:33) at RemoteQuery.query (/home/donielmoins/medusa-eats/backend/node_modules/@medusajs/modules-sdk/src/remote-query/remote-query.ts:289:36) at Query.graph (/home/donielmoins/medusa-eats/backend/node_modules/@medusajs/modules-sdk/src/remote-query/query.ts:166:42) at GET (/home/donielmoins/medusa-eats/backend/src/api/uber/delivery/route.ts:48:44) at /home/donielmoins/medusa-eats/backend/node_modules/@medusajs/utils/src/common/wrap-handler.ts:17:20 at Layer.handle [as handle_request] (/home/donielmoins/medusa-eats/backend/node_modules/express/lib/router/layer.js:95:5) at next (/home/donielmoins/medusa-eats/backend/node_modules/express/lib/router/route.js:149:13 `

Link to reproduction repo

https://github.com/DonielMoins/medusa-eats

sradevski commented 4 days ago

@DonielMoins can you please provide more info on where this happens, what you are calling, what you've modified? There are quite a few commits, and we want to make sure if this is an issue with what you've implemented or something else.

DonielMoins commented 4 days ago

@DonielMoins can you please provide more info on where this happens, what you are calling, what you've modified? There are quite a few commits, and we want to make sure if this is an issue with what you've implemented or something else.

This happens in this file: /home/donielmoins/dev/medusa-eats/backend/src/api/uber/delivery/route.ts Specifically, under the GET function below when we run the query.graph

Heres the snippet of code:

import type { MedusaRequest, MedusaResponse } from "@medusajs/framework";
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils";
import zod from "zod";
import { handleUberDeliveryWorkflow } from "src/workflows/uber-delivery/workflows/handle-uber-delivery";
import { createUberDeliveryWorkflow } from "src/workflows/uber-delivery/workflows/create-uber-delivery";
import { UBER_DELIVERY_MODULE } from "src/modules/uber-delivery";
import type UberDeliveryModuleService from "src/modules/uber-delivery/service";

const schema = zod.object({
  cart_id: zod.string().startsWith("cart_"),
  restaurant_id: zod.string().startsWith("res_"),
});

export async function POST(req: MedusaRequest, res: MedusaResponse) {
  const validatedBody = schema.parse(req.body);

  const { result: delivery } = await createUberDeliveryWorkflow(req.scope).run({
    input: {
      cart_id: validatedBody.cart_id,
      restaurant_id: validatedBody.restaurant_id,
    },
  });

  const { transaction } = await handleUberDeliveryWorkflow(req.scope).run({
    input: {
      delivery_id: delivery.id,
    },
  });

  return res
    .status(200)
    .json({ message: "Delivery created", delivery, transaction });
}

export async function GET(req: MedusaRequest, res: MedusaResponse) {
  const query = req.scope.resolve(ContainerRegistrationKeys.QUERY);
  const { take = null, skip, ...queryFilters } = req.query;

  const uberDeliveryQuery = {
    entity: "uber_delivery",
    fields: ["*", "cart.*", "cart.items.*", "order.*", "order.items.*"],
    filters: queryFilters,
    pagination: {
      take: Number.parseInt(take as string) || null,
      skip: Number.parseInt(req.query.skip as string) || 0,
    },
  };
  const { data: deliveries } = await query.graph(uberDeliveryQuery);

  return res.status(200).json({ deliveries });
}
sradevski commented 1 day ago

Ok I see, so this is some custom code you've implemented. As it is not really an issue with the repo I'll close the repo. It is likely an issue with how you've defined your module, check it against our docs: https://docs.medusajs.com/learn/basics/modules