2017-09-15 19 views
0

node.jsに問題があり、AWS APIを使用してRDSインスタンスに関する情報を取得しようとしています。ほとんどの情報は、ページのレンダリング時に返されますが、データベース "タグ"のAPI呼び出しが完了する前に、データベースはページに戻されています。ノードAWS APIを使用したJS非同期呼び出し

var dbInstances = []; 
    var rds = new AWS.RDS(); 
    rds.describeDBInstances({}, function(err, data) { 
    if (err) console.log(err); 
    //loop through all databases that were returned and pull out the info we need. 
    data.DBInstances.forEach(function(dbInstance) { 
     dbInstance.Name = dbInstance.DBInstanceIdentifier; 
     dbInstance.Status = dbInstance.DBInstanceStatus; 

     //we need to look up the tags for the current db instance. 
     rds.listTagsForResource({ResourceName: dbInstance.DBInstanceArn}, 
     function(tagerr, tagdata) { 
     //loop through all the tags of the specified db instance. 
     if (tagerr) console.log(tagerr); 
     if (tagdata !== null) { 
      var tags = tagdata.TagList || []; 
      tags.forEach(function(tag) { 
      //Look to see if the database has the "Group" tag 
      if (tag.Key === "Group") { 
       dbInstance.Group = tag.Value; 
      } 
      }); 
     } 
     }); 
     //put it in our databases array. 
     dbInstances.push(dbInstance); 
    }); 
    //prepare view model 
    var viewObj2 = { 
     "region": regionObj.Name, 
     "dbInstances": dbInstances 
    }; 
    //render view model. 
    res.render('instances', viewObj2); 
    }); 
}); 

ページがレンダリングされると、私は以下の情報があります:ノードの方法は、「RDSの前に、データベースの配列を返すので、あなたが見ることができるように

NAME  GROUP  STATUS 
Db1     running 
Db2     stopped 
Db3     running 
.... 

を、私の「グループ」の情報が不足しているすべてであります.listTagsForResource "呼び出しが完了しました。私はコード内の他の非同期関連のバグを見つけ出すことができ、それらに対処するためにコールバックを使用しました。しかし、コードのこの特定のチャンクでコールバックをどのように設定しても、必要なデータが得られません。

どうすればこの作品を作成できますか?

はい、私はdbInstanceタグがログに記録されていることを確認しました。 :)

答えて

1

promise variants in the SDKをご覧ください。 rds.listTagsForResource(res).promise()を使用して、標準Promise.all()を使用して、約束が達成されるのを待ちます。

+0

これは私が必要としていたものです。ありがとうございました! – TaylorB

関連する問題