question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Invalid argument provided in `Service` constructor. Expected class annotated with @Service.

See original GitHub issue

image

Hello, I’m getting this error: TypeError: Invalid argument provided in DatabaseService's constructor. Expected class annotated with @Service.

I usually get this error when I have two services that have dependencies on each other, I mean service A is injected into service B and viceversa.

Right now I have this error because I import Logger Service from index.js and not from logger.service.ts

With this I get the error shown above: image

Changing the import I don’t get the error: image

As I said before, this error happens to me very often when service A has service B as a dependency and vice versa.

By the way, my logger.service.ts:

import {FastifyInstance} from 'fastify';
import {
  Service,
  getInstanceByToken,
  FastifyInstanceToken,
} from 'fastify-decorators';

@Service('LoggerServiceToken')
export class LoggerService {
  private readonly fastify =
    getInstanceByToken<FastifyInstance>(FastifyInstanceToken);

  info(msg: string, ...args: unknown[]) {
    this.fastify.log.info(msg, ...args);
  }

  error(msg: string, ...args: unknown[]) {
    this.fastify.log.error(msg, ...args);
  }

  debug(msg: string, ...args: unknown[]) {
    this.fastify.log.debug(msg, ...args);
  }

  warn(msg: string, ...args: unknown[]) {
    this.fastify.log.warn(msg, ...args);
  }

  trace(msg: string, ...args: unknown[]) {
    this.fastify.log.trace(msg, ...args);
  }

  fatal(msg: string, ...args: unknown[]) {
    this.fastify.log.fatal(msg, ...args);
  }
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
L2jLigacommented, Nov 4, 2022

Hi @akbarsahata, with BaseWorker I think issue can be constructor parameters, dependency injection expects constructable however in your constructor I see configuration, I would suggest trying following approach (without using constructor):

class BaseWorker<T> {
  protected workerName = '';
  protected concurrency = 1;
}

class CoopUpdateWorker extends BaseWorker<Coop> {
  protected workerName = QUEUE_COOP_UPDATED;
}
1reaction
akbarsahatacommented, Nov 4, 2022

Hi @akbarsahata, with BaseWorker I think issue can be constructor parameters, dependency injection expects constructable however in your constructor I see configuration, I would suggest trying following approach (without using constructor):

class BaseWorker<T> {
  protected workerName = '';
  protected concurrency = 1;
}

class CoopUpdateWorker extends BaseWorker<Coop> {
  protected workerName = QUEUE_COOP_UPDATED;
}

cool, I happened to use constructor such way on some places and have it fixed. thanks @L2jLiga 🙏🏻

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Parameter 0 of constructor in ..... Spring Boot
When you create a bean by the constructor approach, all normal classes are usable by and compatible with Spring. That is, the class...
Read more >
Entity Annotation & EntityRepository Template causes Psalm ...
Bug Report When updating our doctrine/orm dependency to 2.12.1 we experienced Psalm issues of the following kind in our CI pipeline: ERROR: ...
Read more >
Java Method/Constructor in Class Cannot be Applied to Given ...
Whenever a method invocation doesn't match the corresponding method signature, the method X in class Y cannot be applied to given types error...
Read more >
Fixing common type problems - Dart programming language
Fixing common type problems · Undefined member · Invalid method override · Missing type arguments · Unexpected collection element type · Constructor initialization ......
Read more >
Spring BeanCreationException - Baeldung
or in a Java @Configuration class via the @Bean annotation; or is annotated with @Component, @Repository, @Service, @Controller, and classpath ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found