Giter Site home page Giter Site logo

Comments (17)

jeffijoe avatar jeffijoe commented on June 13, 2024

It looks like you are mixing injection modes, but its hard to tell without your container configuration

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

thanks for your reply

here are my container configuration


const container = awilix.createContainer();

class Container {
    constructor(keys) {
        container.register({
            config: `awilix.asClass(config).setInjectionMode(awilix.InjectionMode.CLASSIC).singleton(),`
            authenticationService : awilix.asClass(AuthenticationService),
            authenticationController : awilix.asClass(AuthenticationController),
            DateUtil : awilix.asValue(DateUtil),
            RestUtil : awilix.asClass(RestUtil),
            CommonUtil : awilix.asValue(CommonUtil),
            MailUtil : awilix.asValue(MailUtil),
            MessageUtil : awilix.asValue(MessageUtil),
            OauthUtil : awilix.asClass(OauthUtil),
        }),
        this.resolve = this.resolve.bind(this);
    }
    resolve(key) {
        return container.resolve(key);
    }
}

module.exports = Container;


from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

Yes, as I suspected, you are mixing injection modes which I don’t recommend. One of your modules are likely treating a parameter as some other module but in reality its the container cradle (proxy).

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

so now did some changes in code

const container = awilix.createContainer();
class Container {
    constructor(keys) {
        container.register({
            config: awilix.asClass(config).singleton(),
            authenticationService: awilix.asClass(AuthenticationService),
            authenticationController: awilix.asClass(AuthenticationController),
            RestUtil: awilix.asClass(RestUtil),
            OauthUtil: awilix.asClass(OauthUtil),
            DateUtil: awilix.asValue(DateUtil),
            CommonUtil: awilix.asValue(CommonUtil),
            MailUtil: awilix.asValue(MailUtil),
            MessageUtil: awilix.asValue(MessageUtil),
        }),
            this.resolve = this.resolve.bind(this);
    }
    resolve(key) {
        return container.resolve(key);
  
``

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

Either avoid mixing injection modes or find the module that is assuming CLASSIC but is actually using PROXY (default).

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

so now i am only using proxy as an injection mode but it is giving me the same error

AwilixResolutionError: Could not resolve 'Symbol(Symbol.toPrimitive)'.

Resolution path: Symbol(Symbol.toPrimitive)
``

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

What are you resolving first?

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

i am resoling authenticationController in my authenticationRouter module



const Container = require("../container") ;
const ContainerINST = new Container() ;
const AUTHENTICATIONCONTROLLERINST = ContainerINST.resolve("authenticationController") ;``

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

Then look at the constructor of AuthenticationController, you are likely treating it as CLASSIC even though it's registered as PROXY.

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

here is the constructor of authentication controller


class AuthenticationController {
  constructor({authenticationService, config, DateUtil,RestUtil, CommonUtil, MessageUtil, OauthUtil}) {
    this.DateUtil = DateUtil ;
    this.config = config ;
    // console.log("config here:::::::::::", this.config)
    this.authService = authenticationService;
    // this.authService = new AUTHENTICATIONSERVICE(config);
    this.rbacController = new RBACCONTROLLER(this.config);
    this.restUtil = RestUtil;
    this.OauthUtil = OauthUtil;
    this.CommonUtil = CommonUtil;
    this.MessageUtil = MessageUtil ;

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

What is the full stack trace of the error?

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

so what im doing is im resolving the AuthentiationController from theAuthenticationRoute

const Container = require("../container") ;
const ContainerINST = new Container() ;
const AUTHENTICATIONCONTROLLERINST = ContainerINST.resolve("authenticationController") ;

then im calling FindUserByEmailForLogin function from the AuthentiationController
router.get("/login/:email",checkJwt, AUTHENTICATIONVALIDATIONINST.findUserByEmailForLogin(), result_validator, AUTHENTICATIONCONTROLLERINST.findUserByEmailForLogin);

and then in AuthentiationController constructor im destructing the dependcies

class AuthenticationController {
  constructor({authenticationService, config, DateUtil,RestUtil, CommonUtil, MessageUtil, OauthUtil}) {
    this.DateUtil = DateUtil ;
    this.config = config ;
    // console.log("config here:::::::::::", this.config)
    this.authService = authenticationService;
    // this.authService = new AUTHENTICATIONSERVICE(config);
    this.rbacController = new RBACCONTROLLER(this.config);
    this.restUtil = RestUtil;
    this.OauthUtil = OauthUtil;
    this.CommonUtil = CommonUtil;
    this.MessageUtil = MessageUtil ;

and In FindUserByEmailForLogin im calling a function from OauthUtil (i pass OauthUtil as dependency in AuthentiationController)

class OauthUtil {
    constructor({config, RestUtil, CommonUtil, MessageUtil}) {
        console.log("here in the oath Util")
        this.config = config;
        this.CommonUtil = CommonUtil;
        this.restUtil =  RestUtil;
        this.MessageUtil = MessageUtil;
        this.mongoose = new MONGOOSEDB();

and then In OauthUtil 's Function CreateAccessToken I am using RestUtil which i pass as an dependency in OauthUtil's constructor

  async createAccessToken(type, update_time) {
       try {
           let data = {
               "client_id": this.config.get('client_id'),
               "client_secret": this.config.get('client_secret'),
               "audience": `${this.config.get('audience_management_api')}`,
               "grant_type": "client_credentials"
           };
           let options = {
               headers: { 'content-type': 'application/json' },
           }
           let url = this.config.get('oauth:token_url');
           let response = await this.restUtil.postRequest(url, data, options);
           this.storeAccessToken(type, response.data, update_time, this.MessageUtil.info().oauth_token.management)
           return response.data;
       } catch (err) {
           console.log("err createAcceessToken:::", err)
           throw new Error(err);
       }
   }

when i restUtil Try to call its function PostRequest its is giving me an error

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024
createAcceessToken::: AwilixResolutionError: Could not resolve 'Symbol(Symbol.toPrimitive)'.

Resolution path: Symbol(Symbol.toPrimitive)
    at resolve (/home/kirat/microservices/authentication_managing_microservice/backend/microservice/authentication/node_modules/awilix/lib/container.js:252:23)
    at Object.get (/home/kirat/microservices/authentication_managing_microservice/backend/microservice/authentication/node_modules/awilix/lib/container.js:66:33)
    at parseInt (<anonymous>)
    at dispatchHttpRequest (/home/kirat/microservices/authentication_managing_microservice/backend/node_modules/axios/lib/adapters/http.js:342:21)
    at new Promise (<anonymous>)
    at httpAdapter (/home/kirat/microservices/authentication_managing_microservice/backend/node_modules/axios/lib/adapters/http.js:48:10)
    at dispatchRequest (/home/kirat/microservices/authentication_managing_microservice/backend/node_modules/axios/lib/core/dispatchRequest.js:58:10)
    at Axios.request (/home/kirat/microservices/authentication_managing_microservice/backend/node_modules/axios/lib/core/Axios.js:108:15)
    at Axios.<computed> [as post] (/home/kirat/microservices/authentication_managing_microservice/backend/node_modules/axios/lib/core/Axios.js:140:17)
    at Function.wrap [as post] (/home/kirat/microservices/authentication_managing_microservice/backend/node_modules/axios/lib/helpers/bind.js:9:15)
errr::::::::: Error: AwilixResolutionError: Could not resolve 'Symbol(Symbol.toPrimitive)'.

Resolution path: Symbol(Symbol.toPrimitive)
    at OauthUtil.createAccessToken (/home/kirat/microservices/authentication_managing_microservice/backend/common/utils/Oauth-util.js:61:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async OauthUtil.getAccessToken (/home/kirat/microservices/authentication_managing_microservice/backend/common/utils/Oauth-util.js:31:29)
    at async AuthenticationController.findUserByEmailForLogin (/home/kirat/microservices/authentication_managing_microservice/backend/microservice/authentication/controller/authentication-controller.js:692:19)
undefined Error: Error: AwilixResolutionError: Could not resolve 'Symbol(Symbol.toPrimitive)'.

Resolution path: Symbol(Symbol.toPrimitive)
    at OauthUtil.getAccessToken (/home/kirat/microservices/authentication_managing_microservice/backend/common/utils/Oauth-util.js:40:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async AuthenticationController.findUserByEmailForLogin (/home/kirat/microservices/authentication_managing_microservice/backend/microservice/authentication/controller/authentication-controller.js:692:19)

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

If you look at the stack trace, it'll give you some hints. For example, Axios' dispatchHttpRequest is somehow triggering resolve in Awilix. This likely means you passed in the cradle somewhere to Axios, likely in your restUtil.

Looking at Axios's source code, there is exactly one parseInt in dispatchHttpRequest, which is parsing a timeout config. So my guess is your restUtil is passing in the cradle, which in turn makes parseInt attempt to convert the value to a primitive by x[Symbol.toPrimitive], which then triggers the resolve.

from awilix.

kiratxsinghx avatar kiratxsinghx commented on June 13, 2024

hi jeff thanks for your help my pervious error got resolved..
now what i m trying to do is register dependencies using loadModules

but loadModules is not working as it is not registering any dependencies

my code :

` .loadModules([
            "service/**/*",
            "controller/**/*",
            "routes/**/*",
        ],
            {
                resolverOptions: {
                    lifetime: awilix.Lifetime.SCOPED,
                    register: awilix.asClass,
                },
                formatName: 'camelCase'
            }
        )
        this.resolve = this.resolve.bind(this);
        console.log("In the container.js",container.registrations )
    }`

app structure::

-->app
         -->service
                       ->userService
                       ->anyotherservice
           -->controller
                       ->userController
                       ->anyotherController
          -->routes
                       ->userRoutes
                       ->anyotherRoutes
         -->Contanier.js

ERROR : AwilixResolutionError: Could not resolve 'authenticationRoute'.
any Suggestions or advice :)

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

Have you followed the documentation for how to set up your modules for auto-loading?

from awilix.

jeffijoe avatar jeffijoe commented on June 13, 2024

Closing this since it's not an issue.

from awilix.

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.