The ReceiveMode property is always undefined.
The steps I have followed to troubleshoot the issue:
- clean npm cache/delete node_modules/*.lock file
- installing/verifying the latest node version
- testing with a minimal example
- The debugger on vscode shows peerLock and receiveAndDelete property as available, but it still ends up with an undefined error.
Note: CreateSender and createMessageBatch code works perfectly; ReceiveMode is not working.
Code Sample:
const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus");
async function main() {
// Create a Service Bus client using the connection string to the Service Bus namespace
const sbClient = new ServiceBusClient(connectionString);
// Define topicName and subscriptionName
const topicName = "your_topic_name";
const subscriptionName = "your_subscription_name";
// Create a receiver for the subscription
const receiver = sbClient.createReceiver(topicName, subscriptionName, {
receiveMode: ReceiveMode.peekLock,
prefetchCount: 100, // Set the prefetch count to fetch 100 messages in advance
});
// Function to handle messages
const myMessageHandler = async (messages) => {
try {
for (const message of messages) {
console.log(`Received message: ${message.body}`);
// Process the message here...
// Complete the message to remove it from the subscription
await message.complete();
}
} catch (error) {
console.error("An error occurred while processing the messages:", error);
// Abandon the messages if processing failed to allow them to be processed again
for (const message of messages) {
await message.abandon();
}
}
};
// Function to handle any errors
const myErrorHandler = async (error) => {
console.error("An error occurred while receiving messages:", error);
};
// Subscribe and specify the message and error handlers
receiver.subscribe({
processMessage: myMessageHandler,
processError: myErrorHandler
});
console.log("Receiving messages...");
// Wait for a certain duration before closing the receiver and Service Bus client
await new Promise(resolve => setTimeout(resolve, 60000)); // Wait for 60 seconds
// Close the receiver and Service Bus client
await receiver.close();
await sbClient.close();
}
// Call the main function
main().catch((err) => {
console.error("Error occurred: ", err);
process.exit(1);
});