【问题标题】:node.js using SQS need functional examplenode.js 使用 SQS 需要功能示例
【发布时间】:2020-05-20 02:06:27
【问题描述】:
var params = {
    QueueUrl: sqsEndPoint + compInfoQueName,
    AttributeNames: [All],
    MaxNumberOfMessages: '5',
    MessageAttributeNames: [],
    ReceiveRequestAttemptId: "",
    VisibilityTimeout: '15',
    WaitTimeSeconds: '10'
};

sqs.receiveMessage(params, function (err, data) {
    if (err) console.log(err, err.stack);
    else return data;
});

从 AWS 网站获得

我已经设置了所有参数,并且可以使用以下方法在 html 页面上获得响应:

https://sqs.us-west-2.amazonaws.com/ACCOUNT/QUEUENAME";

但我想将其集成到 node.js 项目中

我是 javascript 新手,在 java 中我只会导入 AWS 对象并在函数中调用一个方法,即。

功能... {json 响应 = sqs.receive... }

如何在 JS 中使用顶部的代码?

如何通过代码示例将字符串对象设置为接收结果?

我只想在一个html页面中设置一个字段到接收到的sqs消息

textarea.value = myMessageFromAWS

【问题讨论】:

标签: javascript node.js amazon-sqs


【解决方案1】:

由于您来自 Java,因此回调不是很直观。

我建议您使用 async/await 方法,这样代码看起来更线性,如下所示:

async function main () {
  var params = {
    QueueUrl: sqsEndPoint + compInfoQueName,
    AttributeNames: [All],
    MaxNumberOfMessages: '5',
    MessageAttributeNames: [],
    ReceiveRequestAttemptId: "",
    VisibilityTimeout: '15',
    WaitTimeSeconds: '10'
  };

  try {
    // this will be your data received from sqs
    const data = await sqs.receiveMessage(params).promise()
    // prints to stdout
    console.log(data)

  } catch(err) {
    // prints to stderr
    console.error(err, err.stack);
  };
}

// Because this is not Java you have to call this function explicitly
main()

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2011-11-08
    • 1970-01-01
    相关资源
    最近更新 更多