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

question: receiving ServiceNotFoundError after trying basic example from documentation #281

Open
alanlee8421 opened this issue Apr 1, 2021 · 3 comments
Labels
flag: needs docs Issues or PRs which still need documentation added. type: question Questions about the usage of the library.

Comments

@alanlee8421
Copy link

const AlanService = require('../../services/alan/alan.service');
const Container = require('typedi').Container;

exports.readAll = async (req, res, next) => {
  try {
    const alanService = Container.get(AlanService);
    const result = await alanService.alan();
    responseJson(req, res, result);
  } catch (error) {
    console.error(error);
    errorHandler(req, res, error, next);
  }
};

error log:

ServiceNotFoundError: Service with "MaybeConstructable<AlanService>" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@Service()" decorator.

This error occurs after version 0.8.0.

Your guide explains that when a service is used as above, it checks for the existence of an instance and creates or retrieves it when .get is called. And the above code worked normally until 0.8.0. Can you explain how the usage has changed from the next version?

스크린샷 2021-04-01 오후 12 36 09

Your official document also doesn't work.

@alanlee8421 alanlee8421 added the type: question Questions about the usage of the library. label Apr 1, 2021
@alanlee8421 alanlee8421 changed the title question: <your-title-goes-here> there is a problem with loading the service from the node.js project, not from the typescript. Apr 1, 2021
@artur-dani
Copy link

Same problem here

@NoNameProvided
Copy link
Member

Sorry, documentation needs an update. In the past, we did attempt to blindly construct anything you request but that was not a good solution. Now you have to register everything first before requesting it from the container.

If the old solution is required for you you can write a miniam abstraction, something like this:

import { ContainedType, ContainerInterface } from 'typeorm';
import { Container, Constructable } from 'typedi';

export class TypeDIContainerProvider implements ContainerInterface {
  get<T>(constructable: ContainedType<T>) {
    /**
     * TypeDI only resolves values for registered types, so we need to register
     * them before to requesting them from the default container.
     */
    if (!Container.has(constructable as Constructable<T>)) {
      Container.set({ id: constructable, type: constructable as Constructable<T> });
    }

    return Container.get(constructable as Constructable<T>);
  }
}

I will leave this issue open until documentation is fixed.

@NoNameProvided NoNameProvided added flag: needs docs Issues or PRs which still need documentation added. type: question Questions about the usage of the library. and removed type: question Questions about the usage of the library. labels Jan 14, 2022
@NoNameProvided NoNameProvided changed the title there is a problem with loading the service from the node.js project, not from the typescript. question: receiving ServiceNotFoundError after trying basic example from documentation Jan 14, 2022
@chrono-jeff
Copy link

chrono-jeff commented May 30, 2023

Your official document also doesn't work.

This issue has been open for 2 years

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: needs docs Issues or PRs which still need documentation added. type: question Questions about the usage of the library.
Development

No branches or pull requests

4 participants