【问题标题】:Get item from dynamodb table using separate index table with Node.js使用带有 Node.js 的单独索引表从 dynamodb 表中获取项目
【发布时间】:2016-03-24 02:59:49
【问题描述】:

我正在使用 Particle Electron 和 AWS 创建一个气象站。我已设法将返回的数据发送到 DynamoDB 表“天气”,该表包含具有以下模式的所有天气数据(包含示例值):

Item{13}
deviceId: 540056000a51343334363138 (String) (Primary Partition Key)
tm: 1458754711 (Number) (Primary Sort Key) 
batSoC: 89 (String)
batV: 4.01 (String)
hum: 27.9 (String)
lat: 41.2083 (String)
lon: -73.3439 (String)
pres: 968.4 (String)
temp: 19.8 (String)
uvI: 0.1 (String)
wDir: 0 (String)
wGst: 0.0 (String)
wSpd: 0.0 (String)

以及一个单独的“weather_index”表,其中仅包含写入主表的最新数据的 deviceIdtm 属性(有点像原子计数器,但用于定期更新的 unix 时间戳价值)。因此,如果上面的“weather_index”项是最新条目,“weather_index”表中的项将如下所示:

Item{2}
deviceIdString: 540056000a51343334363138 (String) (Primary Partition Key)
tmNumber: 1458754711 (Number)

我目前正在尝试在 Node.js 中编写一个非常基本的 Web 前端(在这个项目之前,我没有使用过,所以我仍在学习)并且不知道如何:

  1. 执行 DynamoDB getItem,其中包含通过先前的 getItem 检索到的参数。喜欢:

latestTime = getItem(weather_index, deviceId) // 获取最近一次天气观测的时间“tm”并存储在“latestTime”中 // 其中“weather_index”是表名

currentWeather = getItem(deviceId, tm) // 获取指定“tm”值的天气观测值并将其存储在“currentWeather”中 // 其中“tm”是最近观察的 unix 时间戳

然后我希望能够将各个值打印到终端/网页/信鸽/等...(类似于currentWeather.deviceIdcurrentWeather.tmcurrentWeather.batSoC 等...

我有以下代码无法正常工作:

/*
 * Module dependencies
 */
var AWS = require('aws-sdk')

// weathermon_dev credentials
AWS.config.update({accessKeyId: 'REDACTED for obvious reasons', secretAccessKey: 'This bit too'});

// Select AWS region
AWS.config.update({region: 'us-east-1'});

var db = new AWS.DynamoDB();
// db.listTables(function(err,data) {
//   console.log(data.TableNames);
// });


var time = Date.now() / 1000;
time = Math.round(time);
//console.log("Time: ");
//console.log(time);

time = Math.round(time);



var deviceId = "540056000a51343334363138"

var params = {
  Key: {
    deviceId: {S: deviceId}
  },
  TableName: 'weather_index'
};

var timeJson;

db.getItem(params, function(err,data) {
  if (err) console.log(err); // an error occurred
  else console.log(data); // successful response
    var timeJson = JSON.parse(data);
})

// var timeJson = JSON.parse(data);
// var itemTime = timeJson.item;

console.log("timeJSON: " + timeJson);

// console.log("itemTime: " + itemTime);



var params = {
  Key: {
    deviceId: {S: deviceId},
    time: {N: 'tm'}
  },
  TableName: 'weather'
};


db.getItem(params, function(err, data) {
  if (err) console.log(err); // an error occurred
  else console.log(data); // successful response
})

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js amazon-web-services amazon-dynamodb aws-sdk iot


    【解决方案1】:

    您需要了解 NodeJS 异步调用的工作原理。在执行第二个 getItem() 之前,您需要等到第一个 getItem() 的回调被调用。

    我在这里重写了您代码的相关部分,以向您展示我在说什么,但我建议您尝试理解为什么需要以这种方式编写代码,而不是仅仅复制/粘贴它。

    var deviceId = "540056000a51343334363138"
    
    var params = {
      Key: {
        deviceId: {S: deviceId}
      },
      TableName: 'weather_index'
    };
    
    var timeJson;
    
    db.getItem(params, function(err,data) {
      if (err) console.log(err); // an error occurred
      else {
        console.log(data); // successful response
        var timeJson = JSON.parse(data);
        console.log("timeJSON: " + timeJson);
    
        // Inside this callback we have the weather_index tm value, 
        // so query the weather table here.
        var params = {
          Key: {
            deviceId: {S: deviceId},
            time: {N: 'tm'}
          },
          TableName: 'weather'
        };
    
        db.getItem(params, function(err, data) {
          if (err) console.log(err); // an error occurred
          else {
            console.log(data); // successful response
            // TODO: Use the database response data here
          }
        });
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多