IgorKhomenko / medusa2-marketplace-demo

A demo project of Medusa 2.0 multivendor marketplace
MIT License
7 stars 2 forks source link

I imported the products and the productCreated hook gave me an error. #1

Closed lemon-sandwich closed 3 days ago

lemon-sandwich commented 3 days ago

I'm using the productCreated hook and trying to resolve the "loggedInUser" but I'm getting the following error:

info:    Attempting to send a notification to: '' on the channel: 'feed' with template: 'admin-ui' and data: '{"title":"Product import","description":"Failed to import products from file Untitled spreadsheet - product-import-template (8).csv"}'
error:   import-products:batch-products-as-step:invoke - Could not resolve 'loggedInUser'.

Resolution path: loggedInUser
AwilixResolutionError: Could not resolve 'loggedInUser'.

Resolution path: loggedInUser
    at resolve (/home/kirmada/Developer/upwork/meuraki/node_modules/awilix/src/container.ts:497:15)
    at Object.resolve (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/workflow/local-workflow.ts:105:24)
    at Object.<anonymous> (/home/kirmada/Developer/upwork/meuraki/src/workflows/hooks/product-created.ts:12:36)
    at Object.invoke (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/utils/composer/helpers/create-step-handler.ts:72:67)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async DistributedTransaction.handler (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/workflow/workflow-manager.ts:214:16)
    at async stepHandler (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:806:20)
    at async Promise.allSettled (index 0)
    at async promiseAll (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/utils/src/common/promise-all.ts:23:18)
    at async TransactionOrchestrator.executeNext (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:934:7)
    at async executeNext (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:982:14)
    at async TransactionOrchestrator.resume (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:997:5)
    at async LocalWorkflow.run (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/workflow/local-workflow.ts:371:5)
    at async originalExecution (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:113:25)
    at async newRun (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:194:12)
    at async Function.exportedWorkflow.run (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:424:12)
    at async Object.async.container.container (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/utils/composer/create-workflow.ts:198:29)
    at async Object.invoke (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/utils/composer/helpers/create-step-handler.ts:72:52)
    at async DistributedTransaction.handler (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/workflow/workflow-manager.ts:214:16)
    at async stepHandler (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:806:20)
    at async Promise.allSettled (index 0)
    at async promiseAll (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/utils/src/common/promise-all.ts:23:18)
    at async TransactionOrchestrator.executeNext (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:934:7)
    at async executeNext (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:982:14)
    at async TransactionOrchestrator.resume (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:997:5)
    at async LocalWorkflow.run (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/workflow/local-workflow.ts:371:5)
    at async originalExecution (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:113:25)
    at async newRun (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:194:12)
    at async Function.exportedWorkflow.run (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/helper/workflow-export.ts:424:12)
    at async Object.async.container.container (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/utils/composer/create-workflow.ts:198:29)
    at async Object.invoke (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/workflows-sdk/src/utils/composer/helpers/create-step-handler.ts:72:52)
    at async DistributedTransaction.handler (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/workflow/workflow-manager.ts:214:16)
    at async stepHandler (/home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:860:20)
⮑ sat /home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/core-flows/dist/product/workflows/create-products.js: [create-products -> productsCreated (invoke)]
⮑ sat /home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/core-flows/dist/product/workflows/batch-products.js: [batch-products -> create-products-as-step (invoke)]
⮑ sat /home/kirmada/Developer/upwork/meuraki/node_modules/@medusajs/core-flows/dist/product/workflows/import-products.js: [import-products -> batch-products-as-step (invoke)]

I have registered the loggedInUser in the middleware and added it as a middleware in my product routes.

Medusa version: "latest" Node: 20.17.0

lemon-sandwich commented 3 days ago

I figured it out. I changed

  const user_id = req.session?.auth_context?.actor_id
  if (!user_id) {
    next();
    return;
  }

to this.

  const user_id = req.session?.auth_context?.actor_id?? req.auth_context.actor_id
  if (!user_id) {
    next();
    return;
  }

Sometimes, the user_id is stored in session and sometimes in auth_context.