简体   繁体   English

如何将消息发送到我的队列和发布请求?

[英]How can I send messages to my queue and a post request?

I have a function that posts data to the database. 我有一个将数据发布到数据库的功能。 It works fine but I would also like to use the same function to send a message to trigger another function. 它工作正常,但我也想使用相同的功能发送消息来触发另一个功能。

I have tried to simultaneouslysend the message and make the post request but at this moment only the post request works 我试图同时发送消息并发出帖子请求,但此时只有帖子请求有效

Here is what my code looks like 这是我的代码的样子

const params = {
    "TableName": "sites",
    "Item": {
      userId: event.requestContext.identity.cognitoIdentityId,
      siteId: siteIdFinal,
      ...data,
      createdAt: Date.now()
    }
  };

  const messageParams = {
    MessageBody: 'Waddup fam',
    QueueUrl: ' https://sqs.eu-west-1.amazonaws.com/106845550704/MyQueue'
  };


  try {
    await dynamoDbLib.call("put", params);
    sqs.sendMessage(messageParams, (err, data) => {
      if (err) {
        console.log("Error: "+err);
      } else {
        console.log("Success: "+data.MessageId);
      }
    });

    return success(params.Item);
  } catch (e) {
    console.log(e);
    return failure({ status: false });
  }

I am not getting any error I am just getting returned back the data that has been posted. 我没有收到任何错误我只是返回已发布的数据。 I thought i should receive the message Id of the message I sent but I am not getting that. 我以为我应该收到我发送的消息的消息ID,但我没有得到。 When ever I look at the cloudwatch logs, the message isnt sent 当我查看cloudwatch日志时,不会发送消息

Your async function returns params.Item before sendMessage executes a callback. 您的异步函数在sendMessage执行回调之前返回params.Item Use a promise to make sure that both methods finish properly 使用promise来确保两种方法都正确完成

await sqs.sendMessage(messageParams).promise()
    .then(function(data) {
        console.log("Success: "+data.MessageId);
    }).catch(function(err) {
        console.log("Error: "+err);
  });

More on aws-sdk and promises: https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/ 有关aws-sdk和promises的更多信息: https//aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM