GithubHelp home page GithubHelp logo

Comments (8)

bibekkakati avatar bibekkakati commented on May 21, 2024 1

Hey, it's working. I just updated the dependencies with exact version as in the package.json of the tutorial-repo.

Thanks @adrien2p

from medusa-extender.

adrien2p avatar adrien2p commented on May 21, 2024

Hi there,

Could you show me your components where you are trying to access the loggedInUser. Also, i can see that you are applying it everywhere. The store routes have an optional auth system which means than the loggedInUser can be undefined.

I suggest you to look at this you can see here that the loggedInUser will always be part of the container but can be null in some cases

from medusa-extender.

bibekkakati avatar bibekkakati commented on May 21, 2024

I tried by installing the medusa-marketplace package as well.

Main error was occurring while trying to add a product.

This is my product.service.ts

import {
  EntityEventType,
  MedusaEventHandlerParams,
  OnMedusaEntityEvent,
  Service,
} from "medusa-extender";

import { EntityManager } from "typeorm";
import { ProductService as MedusaProductService } from "@medusajs/medusa/dist/services";
import { Product } from "./product.entity";
import { User } from "../user/user.entity";
import UserService from "../user/user.service";

type ConstructorParams = {
  manager: any;
  loggedInUser: User;
  productRepository: any;
  productVariantRepository: any;
  productOptionRepository: any;
  eventBusService: any;
  productVariantService: any;
  productCollectionService: any;
  productTypeRepository: any;
  productTagRepository: any;
  imageRepository: any;
  searchService: any;
  userService: UserService;
  cartRepository: any;
  priceSelectionStrategy: any;
};

@Service({ scope: "SCOPED", override: MedusaProductService })
export class ProductService extends MedusaProductService {
  readonly #manager: EntityManager;

  constructor(private readonly container: ConstructorParams) {
    super(container);
    this.#manager = container.manager;
  }

  @OnMedusaEntityEvent.Before.Insert(Product, { async: true })
  public async attachStoreToProduct(
    params: MedusaEventHandlerParams<Product, "Insert">
  ): Promise<EntityEventType<Product, "Insert">> {
    const { event } = params;
    const loggedInUser = this.container.loggedInUser;
    event.entity.store_id = loggedInUser.store_id; // getting loggedInUser as null
    return event;
  }

  prepareListQuery_(selector: object, config: object): object {
    const loggedInUser = this.container.loggedInUser;
    if (loggedInUser) {
      selector["store_id"] = loggedInUser.store_id;
    }

    return super.prepareListQuery_(selector, config);
  }
}

from medusa-extender.

adrien2p avatar adrien2p commented on May 21, 2024

So, as you can see in the tutorial, we now always register the loggedInUser but as a nullable value instead of registering it only when there is a loggedInUser. Can I ask you to update your middleware with the one in the tutorial repo?

from medusa-extender.

bibekkakati avatar bibekkakati commented on May 21, 2024

I replaced the modules folder from the tutorial-repo but getting same error again. Not sure what is happening.

error:   Cannot read properties of null (reading 'store_id')
TypeError: Cannot read properties of null (reading 'store_id')

from medusa-extender.

adrien2p avatar adrien2p commented on May 21, 2024

could you share me a reproducible repo so that I can see the config as well as the code please

from medusa-extender.

Keith-Hon avatar Keith-Hon commented on May 21, 2024

Hi @adrien2p I have exactly the same problem. I do not want to downgrade the medusa's version tho. Is there any documentation on how and where req.user is resolved from a request?

Here are the versions of the packages used
"@medusajs/medusa": "1.3.5",
"@medusajs/medusa-cli": "1.3.1",
"medusa-extender": "1.7.4",

from medusa-extender.

Keith-Hon avatar Keith-Hon commented on May 21, 2024

Hi @adrien2p I have exactly the same problem. I do not want to downgrade the medusa's version tho. Is there any documentation on how and where req.user is resolved from a request?

Here are the versions of the packages used "@medusajs/medusa": "1.3.5", "@medusajs/medusa-cli": "1.3.1", "medusa-extender": "1.7.4",

I ended up fixing it by

  if (req.user && req.user.userId) {
      userId = req.user.userId;
  } else if (req.session.jwt) {
      const decoded = jwt_decode(req.session.jwt);
      userId = decoded['userId'];
  }

  if (userId) {
      const userService = req.scope.resolve('userService') as UserService;
      const loggedInUser = await userService.retrieve(userId, {
          select: ['id', 'store_id'],
      });
      req.scope.register({
          loggedInUser: {
              resolve: () => loggedInUser,
          },
      });
  }

from medusa-extender.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.