2016-03-23 9 views
1

Particle ElectronとAWSを使用して気象ステーションを作成しています。Node.jsと別のインデックステーブルを使用して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」表:私は(含まれているサンプル値を持つ)以下のすべてのスキーマを持つ気象データの含まれていDynamoDBのテーブル「天気」に送信された返されたデータを取得するために管理していますメインテーブルに書き込まれた最新のデータ(アトミックカウンタのようなものですが、定期的に更新されるunixタイムスタンプ値のもの)の属性はdeviceIdtmです。したがって、上記の「weather_index」の項目は、最新のエントリだった場合、「weather_index」テーブル内の項目は次のようになります。

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

を私は現在、(Node.jsの中に非常に基本的なWebフロントエンドを記述しようとしていますこれは、このプロジェクトに先立って、私はとの経験がなかったしているので、私はまだ学んでいます)、どのように把握することはできません。以前のgetItemを経由して取得したパラメータが含まれてDynamoDBののgetItemを実行し

  1. 。同様に:
  2. 最新の気象観測と "latestTime" に保存するの

latestTime =のgetItem(weather_index、デバイスID)//が時刻を取得し、 "TM" // "weather_indexは、" テーブル名

です

currentWeather = getItem(deviceId、tm)//指定された "tm"値の気象観測を取得して "currentWeather"に格納します // "tm"は最新の観測のunixタイムスタンプです

私は、ターミナル/ウェブページ/キャリアのハト/ etcに個々の値を出力したいと思っています。( 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 
}) 

を任意の助けいただければ幸いです。

答えて

0

NodeJS非同期呼び出しの動作を調べる必要があります。 2番目の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 
     } 
    }); 
    } 
}); 
関連する問題