Home Articles

使用node.js和express将数据从dynamoDB本地表列表到html

Asked
Viewed 1777 times
0

我正在尝试使用node.js和express将DynamoDB本地项目列表到html表,DynamoDB数据如下所示:

{"Items": [{ "id": "A004","name": "ACC LR2","userId": ["1","2","3","4"], {"id": "0001","name": "ABG IT","userId": [ "8","9","10","11"]}}]}

我的nodejs路由是这样的:

router.get('/groups', function(req, res, next) {
      var params = {
    TableName: 'userGroup',

};
dynamodb.scan(params, function(err, data) {
    if (err) {
      console.log(err);
    }
    else { 
        console.log("These are Groups: "+ console.log(JSON.stringify(data.Items)));
     res.render('groups',{_uG : data.Items});

  }
}); 

});

我在html上的表是这样的:

<table>
    <thead>
        <th>Id</th>
        <th>name</th>
        <th>user id</th>
    </thead>
  <tbody>     
    <% for(var i = 0; i < _uG.length; i++) { %>                           
  <tr class="gradeA even" role="row">
            <td class="sorting_1"><%= _uG[i].id.S %></td>
            <td><%= _uG[i].name.S %></td>
            <td><%= _uG[i].userId[i].L %></td>
  </tr>
         <% } %>
      </tbody>
</table>

console.log显示如下结果:

{"id":{"S":"A004"},"name":{"S":"ACC LR2"},"userId":{"L":[{"S":"7"},{"S":"8"},{"S":"9"},{"S":"10"},{"S":"11"}]}}

我能够列出id和name的项目,但是我无法列出userID,我该怎么做才能在html表上列出userID? ..帮助表示赞赏..

1 Answer

  • 0

    看起来userId是一个对象数组,因此您需要迭代它并获取每个对象的字符串值 . 所以我会改变你将userId渲染到这样的行:

    <td><%= _uG[i].userId.L.map(function(item) {return item.S;}).join('') %></td>
    

Related