Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

node.js - AWS Lambda Javascript JSON object undefined

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Change

event.TagData.Time;

To

event.TagData[0].Time;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...