{ const container = -6ren">
gpt4 book ai didi

javascript - Awilix 中的错误 "Could not resolve container"

转载 作者:行者123 更新时间:2023-11-30 14:36:43 24 4
gpt4 key购买 nike

我知道那个错误说明了一切,但实际上我对 Awilix 不是很好。

这是我的 loader/index.js:

export default () => {
const container = createContainer();

container.loadModules([
['services/*.js', { register: asClass }],
['controllers/routes/*.js', { register: asClass }],
['global-controllers/*.js', { register: asFunction }]
], {
formatName: 'camelCase',
resolverOptions: {
lifetime: Lifetime.SINGLETON
}
});

container.register({
apiController: asFunction(apiController).singleton(),
schemas: asFunction(schemas)
});

container.register({
errors: asValue(errors),
Sequelize: asValue(Sequelize),
context: asFunction(context)
});

return container;
};

我是这样用的:

export default async (container) => {
await container.resolve('context').sequelize.sync({ force: true });

const app = express();

app.use('/static', express.static(join(__dirname, './api/views/')));
app.use(cookieParser(key));
app.use(json());

app.use('/api', container.resolve('cacheGlobal'));
app.use('/api', container.resolve('apiController'));
app.use('/api', container.resolve('errorGlobal'));

return app;
}

完整的错误信息是:

UnhandledPromiseRejectionWarning: AwilixResolutionError: Could not resolve 'cacheGlobal'."

我想可能是名称解析问题,我尝试了几种方法,一旦成功!差一点,Router.use() 得到了对象,但它仍然崩溃了。

最佳答案

是的,这是一个与路径相关的问题,只需更改一下:

['services/*.js', { register: asClass }],
['controllers/routes/*.js', { register: asClass }],
['global-controllers/*.js', { register: asFunction }]

对此:

['./api/services/*.js', { register: asClass }],
['./api/controllers/routes/*.js', { register: asClass }],
['./api/global-controllers/*.js', { register: asFunction }]

关于javascript - Awilix 中的错误 "Could not resolve container",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com