Search code examples
node.jsamazon-sqsaws-sdk-js

My SQS queue doesn't show any messages in flight from aws-sdk with node.js


My code is

const AWS = require('aws-sdk')
AWS.config.update({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY })
const sqs = new AWS.SQS({ region: 'us-east-1' })

  return Promise.map(utxos, (utxo) => {
    console.log({
      QueueUrl: process.env.SQS_URL,
      MessageBody: {
        txid: utxo.txid,
        CurrencyId: CurrencyId,
        address: utxo.address
      }
    })

    return sqs.sendMessage({
      QueueUrl: process.env.SQS_URL,
      MessageBody: JSON.stringify({
        txid: utxo.txid,
        CurrencyId: CurrencyId,
        address: utxo.address
      })
    }, (err, response) => {
      console.log('err', err)
      console.log('response', response)
    })
  })

However, when I look in my aws console, I don't see any messages. I'm using a standard queue, not a FIFO one.

What could be wrong?

The 2 console.log's never print. But the first one does with all the correct parameters.


Solution

  • I believe the issue is that you are not returning in a promise inside the Promise.map

    wrapping your sqs code like this should work

    return new Promise((resolve, reject)=>{
      sqs.sendMessage({
        QueueUrl: process.env.SQS_URL,
        MessageBody: JSON.stringify({
          txid: utxo.txid,
          CurrencyId: CurrencyId,
          address: utxo.address
        })
      }, (err, response) => {
        console.log('err', err)
        console.log('response', response)
        if(err)(reject(err))
        resolve(response);
      })
    });