Hi,
its probably something stupid but Im struggling with that for second day, unable to find some working solution.
I have mqttjs client listening subscribed to topic and want to update collection when message is received, but if I use findOne in method called from onMessage listener, I always get “Fatal error occurred: MongoNotConnectedError: Client must be connected before running operations”
const connectDB = async () => {
try {
await client.connect();
db = client.db(DBNAME);
sensors = db.collection('sensors');
} catch (e) {
console.error(e);
}
}
...
mqttClient.on("message", (topic, message) => {
handleMessage(topic, message);
});
...
const handleMessage = (topic, message) => {
... extract sensorId ..
sensors.findOne({ id: sensorId })
.then(result => console.log(`handleMessage ${result}`))
.catch(err => console.error(`Fatal error occurred: ${err}`)); <==== MongoNotConnectedError
}
I tried promise variant because on message is not async, connect call is fine, same method works when used in other locations, just here in message call I’ve got not connected client errror.
Thanks for any hint!
Roman