Nest: Execution Context: app.select(XxxModule) return null

Created on 12 Mar 2018  路  3Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

failed for Execution Context follow the document.
app.select(XxxModule) return null.

Expected behavior

app.select(XxxModule) return XxxModule

Minimal reproduction of the problem with instructions

app.module.ts

import { Module } from "@nestjs/common";
import { TradeModule } from "./trade/trade.module";
import { SharedModule } from "./shared/shared.module";
import env from './app.env'

@Module({
    imports: [
        SharedModule.forRoot(env),
        TradeModule.forRoot(env)
    ],
    exports: [
        TradeModule
    ]
})
export class ApplicationModule { }

test_script.ts

import { NestFactory } from "@nestjs/core";
import { ApplicationModule } from "../../src/app.module";
import { TradeModule } from "../../src/trade/trade.module";
import { MarketService } from "../../src/trade/services";
import { SharedModule } from "../../src/shared/shared.module";

const run = async () => {
    const app = await NestFactory.createApplicationContext(ApplicationModule);
    const tasksController: MarketService = app.select(TradeModule).get(MarketService);
    tasksController.test();
}

run();

error

20:25 $ ts-node script/app/context.ts
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [NestFactory] Starting Nest application...
(node:47194) Warning: N-API is an experimental feature and could change at any time.
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [InstanceLoader] ApplicationModule dependencies initialized +37ms
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [InstanceLoader] SharedModule dependencies initialized +2ms
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [InstanceLoader] MongooseModule dependencies initialized +2ms
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [InstanceLoader] MongooseCoreModule dependencies initialized +144ms
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [InstanceLoader] TradeModule dependencies initialized +38ms
[Nest] 47194   - 2018/3/12 涓嬪崍8:32:33   [InstanceLoader] MongooseModule dependencies initialized +1ms
(node:47194) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'get' of null
    at Object.<anonymous> (/opt/dev/hume2/script/app/context.ts:9:67)
    at Generator.next (<anonymous>)
    at fulfilled (/opt/dev/hume2/script/app/context.ts:4:58)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)
(node:47194) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside ofan async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:47194) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections thatare not handled will terminate the Node.js process with a non-zero exit code.

What is the motivation / use case for changing the behavior?

Environment


Nest version: 4.6.4


question 馃檶

Most helpful comment

The reason is that you, in fact, do not have a base TradeModule anywhere. In your code, you only create a dynamic module that extends a TradeModule (.forRoot() call). Therefore, you should rather use the following syntax:

app.select(TradeModule.forRoot(env)).get(MarketService);

Which is a little bit weird, but proper in this case.

To avoid repeating yourself twice (in app.module.ts and test_script.ts) you can create a single, dedicated file that exports dynamically created a module, for example:

export const EnvTradeModule = TradeModule.forRoot(env);

Afterwards, reuse it everywhere.

Since this syntax is still troublesome a little, in the next release (v5.0.0) you'll be able to get() particular instance without necessity to proceed over whole modules tree (just app.find(MarketService)).

All 3 comments

The reason is that you, in fact, do not have a base TradeModule anywhere. In your code, you only create a dynamic module that extends a TradeModule (.forRoot() call). Therefore, you should rather use the following syntax:

app.select(TradeModule.forRoot(env)).get(MarketService);

Which is a little bit weird, but proper in this case.

To avoid repeating yourself twice (in app.module.ts and test_script.ts) you can create a single, dedicated file that exports dynamically created a module, for example:

export const EnvTradeModule = TradeModule.forRoot(env);

Afterwards, reuse it everywhere.

Since this syntax is still troublesome a little, in the next release (v5.0.0) you'll be able to get() particular instance without necessity to proceed over whole modules tree (just app.find(MarketService)).

expect to v5.0.0

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tronginc picture tronginc  路  3Comments

cojack picture cojack  路  3Comments

KamGor picture KamGor  路  3Comments

anyx picture anyx  路  3Comments

cdiaz picture cdiaz  路  3Comments