本文介绍了AWS Lambda Javascript JSON对象未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的lambda函数由IoT规则(JSON中的MQTT消息)调用.我只是尝试记录这些值,并且顶级点字段可以正常工作,但JSON中的嵌套对象被视为未定义".我试图对它们进行JSON.stringify,但没有成功.有什么想法吗?

My lambda function is called with by an IoT rule (MQTT message in JSON). I am simply trying to log the values, and the top level dot fields work fine, but nested objects in the JSON are seen as "undefined". I tried to JSON.stringify these with no success. Any ideas?

'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
  console.log('Received event:', JSON.stringify(event, null, 2)); // Complete message
  console.log('Received event.ApiVersion:',event.ApiVersion);
  console.log('Received event.CollectionID:',event.CollectionId);
  console.log('Received event.TagData.Time:',event.TagData.Time); //undefined

  var TimeObj = {};
  TimeObj = event.TagData.Time;
  console.log('Received event TimeObj:',TimeObj); //undefined
};

这是云监视日志/结果:

Here are the cloud watch logs / results:

Loading function
Received event:
{
 "FormatId": "TagValues",
 "ApiVersion": 1,
 "CollectionId": 2,
 "TagData": [
     {
         "Time": "2017-09-02T11:06:35.917000+02:00",
         "Values": {
             "var1": 16777216,
             "var2": 7534
         }
     }
 ]
}
Received event.ApiVersion: 1
Received event.CollectionID: 2
Received event.TagData.Time: undefined
Received event TimeObj: undefined

推荐答案

TagData是一个数组而不是对象,因此TagData.Time返回undefined.

TagData is an array and not an object, so TagData.Time returns undefined.

更改

event.TagData.Time;

收件人

event.TagData[0].Time;

这篇关于AWS Lambda Javascript JSON对象未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:02