2016-08-24 7 views
0

Node.jsプロジェクトでうまく動作するSequelizeベースのコードがあります。私はそのコードをAWS Lambdaハンドラに移し、node-lambdaモジュールでテストしています。今Sequelizeコードはスキップされているようです。ラムダが終わる前に約束が処理されていないのか、何か他のものが欠けているのかどうかは分かりません。次のコードは、以下の出力に示すように、console.logの "during"をスキップします。AWSラムダ内でコードが実行されないようにする

var models = require('./models'); 

exports.handler = function(event, context, callback) { 
    console.log("Before"); 

    var body = JSON.parse(event.body); 

    // Find the device ID from the devices table 
    models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) { 
     console.log("During"); 

     // Make sure that we got a device back 
     if (device === null) { 
      console.log("Device not found - could not insert data"); 
      return; 
     } 
     else { 
      console.log("Device found, inserting data - " + body.device_uuid); 

      //Insert the data for this device 
      models.Data.create({ 
       device_id: device.id,    
       data: body.data 
      }); 
     } 
    }); 

    console.log("After"); 

    callback(null, "{\"status\": \"success\"}"); 
} 

利回り...私が間違っているつもりだどこに

Before 
After 
Success: 
"{\"status\": \"success\"}" 

任意のアイデア?私はノードv5.9.0を使用しています。

+0

私は、Sequelizeコードがうまく動作するコールバック参照を削除すると、わかりました。 – user2174937

答えて

3

私はapigateway/lambdaで再生を開始して後に続行しましたが、ノードを知っている限り、コールバックは "then"ブロック内になければなりません。

昨日、コールバック(null、successData)を使用している場合、パフォーマンスは実際には劣っていました(Select Top 1で11秒を超える)。 context.callbackWaitsForEmptyEventLoop = falseフラグを変更しなければならなかったので、api呼び出しに24msかかる。

//Important to improve performance! 
    context.callbackWaitsForEmptyEventLoop = false 

    // Find the device ID from the devices table 
    models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) { 
     console.log("During"); 

     // Make sure that we got a device back 
     if (device === null) { 
      callback(new Error("Device not found - could not insert data")) 
     } 
     else { 
      console.log("Device found, inserting data - " + body.device_uuid); 

      //Insert the data for this device 
      models.Data.create({ 
       device_id: device.id,    
       data: body.data 
      }) 
      .then(function(insertedData){ 
       callback(null, insertedData.toJSON())  
      }) 
      .catch(function(error){ 
       callback(new Error("Error creating") 
      }) 
     } 
    })  
    console.log("After") 
} 
関連する問題