I have create a channel and then sent a message to a queue using channel.sendToQueue() method and I have also create a channel in other module and try to consume messages from the queue asynchronously. All worked fine but after consuming the message I need to query from database. And the database query returned an unexpected data.
sender code
const connect = await amqplib.connect("amqp://localhost")
const channel = await connect.createChannel()
channel.sendToQueue(newsfeedQueue, Buffer.from(JSON.stringify(user._id))) // user is the content i want to send
receiver code
channel.consume(newsfeedQueue, async (msg) => {
const data = JSON.parse(msg.content)
console.log(" ocnsume data ", data)
const storyList = await Story.find({})
console.log("story list ", storyList)
channel.ack(msg)
})
I got expected data when I have queried from database before consuming the queue
const storyList = await Story.find({})
console.log("story list ", storyList)
// Here got the expected data
channel.consume(newsfeedQueue, async (msg) => {
const data = JSON.parse(msg.content)
console.log(" ocnsume data ", data)
// if i query here didn't get expected data
channel.ack(msg)
})
The problem was solved. The problem was in my test. So the previous code is good enough to run.