Nestjs - 远程服务中没有定义匹配的事件处理程序

问题描述 投票:0回答:2

我正在尝试使用微服务处理来自在线 MQTT 代理的关于主题

test_ack
的消息。但我得到了错误。

There is no matching event handler defined in the remote service.

我的代码:

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '@nestjs/common/enums/transport.enum';
var url = 'mqtt://test.mosquitto.org';

async function bootstrap() {
    const app = await NestFactory.createMicroservice(AppModule, {
        transport: Transport.MQTT,
        options: {
            url: url
        }
    });
    await app.listenAsync();
}
bootstrap();

app.controller.ts

import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
    constructor() {}

    @MessagePattern('test') 
    ackMessageTestData(data:unknown) {
        console.log(data.toString());
        return 'Message Received';
    }
}
mqtt nestjs
2个回答
3
投票

由于我没有编辑权限,所以我将其作为新答案发布。正如上面的答案中提到的。我们必须使用

@EventPattern('test_ack')
.

发布的消息应采用

{data: 'Your message'}
格式,并应在发布前进行序列化,如此处所述。

client.publish('test_ack', JSON.stringify({data: 'test data'}))
.


0
投票

您必须处理所有模式,包括未定义类型 @MessagePattern(未定义)

© www.soinside.com 2019 - 2024. All rights reserved.