MQTT + NestJS在直接运行NodeJS时出现TypeError

huangapple go评论95阅读模式
英文:

MQTT + NestJS TypeError when running with NodeJS directly

问题

以下是您要翻译的内容:

  1. # NestJs + MQTT error when trying to host with PM2/NodeJS
  2. 我正在尝试使用NestJs中的ControllerMessagePatterns来处理一些MQTT主题上的消息。我还在使用一个NX monorepo。当我运行以下命令时,一切都正常运行:

yarn nx serve api

  1. 我正在制定部署策略,当我使用以下命令编译我的项目时,一切都正常:

yarn nx run-many --t=build --configuration=production

  1. 问题出现在当我尝试运行编译后的项目时:

node ./dist/apps/api/main.js

  1. 这会导致以下错误消息:

/home/user/Desktop/project/node_modules/mqtt/lib/client.js:701
const resubscribe = obj.resubscribe
^

TypeError: Cannot read properties of undefined (reading 'resubscribe')
at MqttClient.subscribe (/home/user/Desktop/project/node_modules/mqtt/lib/client.js:701:27)
at /home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:40:24
at Array.forEach (<anonymous>)
at ServerMqtt.bindEvents (/home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:38:28)
at ServerMqtt.start (/home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:32:14)
at ServerMqtt.listen (/home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:24:18)
at /home/user/Desktop/project/node_modules/@nestjs/microservices/nest-microservice.js:107:25
at new Promise (<anonymous>)
at NestMicroservice.listen (/home/user/Desktop/project/node_modules/@nestjs/microservices/nest-microservice.js:106:16)

  1. # 我已尝试了以下方法(但没有成功)
  2. - 不同版本的NodeJS(目前是v18.16.0
  3. - 不同版本的MQTT
  4. - 注释掉代码的某些部分:
  5. 以下是我main.ts文件的代码片段:

const mqtt_app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.MQTT,
options: {
url: process.env.MQTT_URL,
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
},
});
mqtt_app.listen();

  1. 如果我不调用```mqtt_app.listen();```,那就没有问题,但这也意味着与MQTT相关的任何内容都不起作用。
  2. 我的mqtt.controller文件:

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

英文:

NestJs + MQTT error when trying to host with PM2/NodeJS

I am trying to use a Controller with MessagePatterns in NestJs to handle messages on some MQTT topics. I am also using an NX monorepo. Everything works fine when I run:

  1. yarn nx serve api

I am working on a deployment strategy, and when I compile my project using:

  1. yarn nx run-many --t=build --configuration=production

Everything is compiling fine.

The issue comes in when I try to run the compiled project using

  1. node ./dist/apps/api/main.js

This causes the following error-message

  1. /home/user/Desktop/project/node_modules/mqtt/lib/client.js:701
  2. const resubscribe = obj.resubscribe
  3. ^
  4. TypeError: Cannot read properties of undefined (reading &#39;resubscribe&#39;)
  5. at MqttClient.subscribe (/home/user/Desktop/project/node_modules/mqtt/lib/client.js:701:27)
  6. at /home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:40:24
  7. at Array.forEach (&lt;anonymous&gt;)
  8. at ServerMqtt.bindEvents (/home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:38:28)
  9. at ServerMqtt.start (/home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:32:14)
  10. at ServerMqtt.listen (/home/user/Desktop/project/node_modules/@nestjs/microservices/server/server-mqtt.js:24:18)
  11. at /home/user/Desktop/project/node_modules/@nestjs/microservices/nest-microservice.js:107:25
  12. at new Promise (&lt;anonymous&gt;)
  13. at NestMicroservice.listen (/home/user/Desktop/project/node_modules/@nestjs/microservices/nest-microservice.js:106:16)

I have tried the following (with no success)

  • Different NodeJS versions (Currently on v18.16.0)
  • Different versions of the MQTT library
  • Commenting out certain parts of code:

Here is a snippet from my main.ts file:

  1. const mqtt_app = await NestFactory.createMicroservice(AppModule, {
  2. transport: Transport.MQTT,
  3. options: {
  4. url: process.env.MQTT_URL,
  5. username: process.env.MQTT_USERNAME,
  6. password: process.env.MQTT_PASSWORD,
  7. },
  8. });
  9. mqtt_app.listen();

If I don't call mqtt_app.listen();, I don't have an issue, but that also means that nothing related to MQTT will work.

My mqtt.controller file:

  1. import { Controller } from &#39;@nestjs/common&#39;;
  2. import { MessagePattern, Payload } from &#39;@nestjs/microservices&#39;;
  3. @Controller()
  4. export class MqttController {
  5. @MessagePattern(process.env.MQTT_TOPIC_1)
  6. async mqtt_a(@Payload() data) {
  7. console.log(`Received ${data} on ${process.env.MQTT_TOPIC_1}`);
  8. return `Received ${data} on ${process.env.MQTT_TOPIC_1}}`
  9. };
  10. @MessagePattern(process.env.MQTT_TOPIC_2)
  11. async mqtt_b(@Payload() data) {
  12. console.log(`Received ${data} on ${process.env.MQTT_TOPIC_2}`);
  13. return `Received ${data} on ${process.env.MQTT_TOPIC_2}}`
  14. }
  15. }

If I comment out the two @MmessagePattern()'s and functions mqtt_a and mqtt_b, everything seems to work fine.
I suspect that when I run this using NodeJS directly, it tries to 'subscribe' to a topic before the microservice is running, or something simillar?

Maybe I am missing something very obvious, but I am out of ideas and any help regarding this will be greatly appreciated!

答案1

得分: 0

问题已解决!
问题出在使用 .env 来定义消息模式

  1. @MessagePattern(process.env.MQTT_TOPIC_2)

已修复,改为使用

  1. @MessagePattern('topic')

不过,这似乎只在使用 NodeJS 运行编译后的代码时存在问题,而在直接使用 NX / NestJS 运行项目时,使用 process.env.* 却可以正常工作。

英文:

Problem solved!

The issue was using .env for the messagepatterns

  1. @MessagePattern(process.env.MQTT_TOPIC_2)

Fixed by rather doing

  1. @MessagePattern(&#39;topic&#39;)

This only seems to be an issue when running the compiled code using NodeJS though, and for some reason using process.env.* works fine when running the project using NX / NestJS directly.

huangapple
  • 本文由 发表于 2023年5月29日 18:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定