浏览 JS 对象
navigate through JS object
我正在尝试浏览 Javascript 对象,但每次出现错误:类型错误:无法读取 属性 'Attributes' of undefined
这是我的代码:
var attributes = [];
var params = {
QueueUrl: 'AMAZON_QUEUE', /* required */
AttributeNames: [
'ApproximateNumberOfMessages',
/* more items */
]
};
setInterval(queueAttributes, 2000)
function queueAttributes () {
sqs.getQueueAttributes(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log (data);
attributes.push(data); // successful response
setTimeout(queueChecker, 1000);
});
}
function queueChecker (attributes) {
if (attributes.Attributes.ApproximateNumberOfMessages == 0) {
console.log ('queueEmpty');
}
else
{
console.log (attributes.Attributes.ApproximateNumberOfMessages + 'messages in queue');
};
}
有人知道我应该如何调用属性变量中包含的 ApproximateNumberOfMessages 值吗?
我从 SQS 得到的响应以及属性变量的内容如下:
{ ResponseMetadata: { RequestId: '6c43d9d6-281d-5bfa-b7f5-94e165b49662' },
Attributes: { ApproximateNumberOfMessages: '0' } }
谢谢。
您正在以数组形式访问属性。
attributes.push(data);
继续在数组属性中添加元素。如果你希望以
的方式访问属性
attributes.Attributes.ApproximateNumberOfMessages
您需要将属性声明为对象:
var attributes = {
Attributes:{
ApproximateNumberOfMessages:0
}
}
然后在您的函数 queueAttributes() 中,您可以按如下方式填充属性对象...
function queueAttributes () {
sqs.getQueueAttributes(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log (data);
//attributes.push(data); // successful response
attributes.Attributes.ApproximateNumberOfMessages = data;
setTimeout(queueChecker, 1000);
});
}
假设要在 attributes.Attributes.ApproximateNumberOfMessages 中填充数据。
希望对您有所帮助!
我正在尝试浏览 Javascript 对象,但每次出现错误:类型错误:无法读取 属性 'Attributes' of undefined
这是我的代码:
var attributes = [];
var params = {
QueueUrl: 'AMAZON_QUEUE', /* required */
AttributeNames: [
'ApproximateNumberOfMessages',
/* more items */
]
};
setInterval(queueAttributes, 2000)
function queueAttributes () {
sqs.getQueueAttributes(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log (data);
attributes.push(data); // successful response
setTimeout(queueChecker, 1000);
});
}
function queueChecker (attributes) {
if (attributes.Attributes.ApproximateNumberOfMessages == 0) {
console.log ('queueEmpty');
}
else
{
console.log (attributes.Attributes.ApproximateNumberOfMessages + 'messages in queue');
};
}
有人知道我应该如何调用属性变量中包含的 ApproximateNumberOfMessages 值吗?
我从 SQS 得到的响应以及属性变量的内容如下:
{ ResponseMetadata: { RequestId: '6c43d9d6-281d-5bfa-b7f5-94e165b49662' },
Attributes: { ApproximateNumberOfMessages: '0' } }
谢谢。
您正在以数组形式访问属性。
attributes.push(data);
继续在数组属性中添加元素。如果你希望以
的方式访问属性attributes.Attributes.ApproximateNumberOfMessages
您需要将属性声明为对象:
var attributes = {
Attributes:{
ApproximateNumberOfMessages:0
}
}
然后在您的函数 queueAttributes() 中,您可以按如下方式填充属性对象...
function queueAttributes () {
sqs.getQueueAttributes(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log (data);
//attributes.push(data); // successful response
attributes.Attributes.ApproximateNumberOfMessages = data;
setTimeout(queueChecker, 1000);
});
}
假设要在 attributes.Attributes.ApproximateNumberOfMessages 中填充数据。
希望对您有所帮助!