我尝试使用@golevelup/nestjs-rabbitmq连接到rabbitmq并使用消息。我想在@RabbitSubscribe方法中使用env变量。它们总是未定义,并搜索解决方案我遇到了这个包:@golevelup/profiguration.
我试着设置这一切,但不让它工作:
import { createProfiguration } from '@golevelup/profiguration';
import { Channel } from 'amqp-connection-manager';
interface Config {
exchange: string;
routingKey: string;
queue: string;
queueOptions: {
durable: boolean;
};
allowNonJsonMessages: boolean;
createQueueIfNotExists: boolean;
errorHandler: () => {};
}
export const subscribeConfig = createProfiguration<Config>({
exchange: { default: 'test', env: process.env.RABBITMQ_EXCHANGE_NAME },
routingKey: { default: 'test', env: process.env.RABBITMQ_ROUTING_KEY },
queue: { default: 'test', env: process.env.RABBITMQ_QUEUE_NAME },
queueOptions: {
durable: { default: true },
},
allowNonJsonMessages: { default: true },
createQueueIfNotExists: { default: true },
errorHandler: (channel: Channel, msg: any, error: Error) => {
console.log(error);
channel.reject(msg, false);
},
});
@RabbitSubscribe({
exchange: subscribeConfig.get('exchange'),
routingKey: subscribeConfig.get('routingKey'),
queue: subscribeConfig.get('queue'),
queueOptions: {
durable: true,
},
allowNonJsonMessages: true,
createQueueIfNotExists: true,
errorHandler: (channel: Channel, msg: any, error: Error) => {
console.log(error);
channel.reject(msg, false);
},
})
public async onQueueConsumption(msg: RequestDto, amqpMsg: ConsumeMessage) {
console.log(msg);
}
当我启动应用程序时,我总是得到这个错误:
C:\Source\app\node_modules\convict\src\main.js:679
throw new Error(output)
^
Error: errorHandler: should be of type Function: value was {}
at Object.validate (C:\Source\app\node_modules\convict\src\main.js:679:17)
at exports.createProfiguration (C:\Source\app\libs\profiguration\src\lib\profiguration.ts:162:28)
at Object.<anonymous> (C:\Source\app\src\app.serviceConfig.ts:17:51)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:827:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Source\app\src\app.service.ts:12:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:827:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
1条答案
按热度按时间iklwldmw1#
我最终没有使用@golevelup/profiguration包。
要在@RabbitSubscribe decorator中使用ENV Variables,您必须创建一个返回RabbitMQ decorator的工厂函数。在那里,您可以初始化ENV值,然后在RabbitSubscribe Config中设置常量并返回此配置。
在AppService中,您可以使用工厂函数在您的服务中生成RabbitMQ装饰器!
如果有人需要env config im使用:ENV Config
下面是代码:
你可以这样使用它: