1

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.

2
  • the database query returned an unexpected data ? try to explain the error Commented Aug 8, 2024 at 4:07
  • story returned empty but if queried before consuming the message , got the expected data Commented Aug 8, 2024 at 4:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.