0

我正在尝试使用 node.js 将 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 ?..帮助表示赞赏..

4

1 回答 1

0

看起来 userId 是一个对象数组,因此您需要对其进行迭代并获取每个对象的字符串值。因此,我会将呈现 userId 的行更改为以下内容:

<td><%= _uG[i].userId.L.map(function(item) {return item.S;}).join('') %></td>
于 2015-11-12T16:54:26.593 回答