Javascript Angular JS和Express项目:将对象传递到ejs视图

Javascript Angular JS和Express项目:将对象传递到ejs视图,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,我有一个Express项目,我正在从Mongo数据库读取数据。我希望在浏览时能够看到数据库中的所有条目 “index.js”视图中的此部分似乎工作正常: app.get('/viewreplies', function(req, res) { // allreplies contains the data from the MongoDB var allreplies = RepliesModel.find().then(function(result){ console.log(

我有一个Express项目,我正在从Mongo数据库读取数据。我希望在浏览时能够看到数据库中的所有条目

“index.js”视图中的此部分似乎工作正常:

app.get('/viewreplies', function(req, res) {

  // allreplies contains the data from the MongoDB
  var allreplies = RepliesModel.find().then(function(result){
  console.log(result);
  return result;
  });
  res.render('replies', { allreplies : allreplies });
});
当我转到localhost:port路径时,console.log命令将完整对象打印到控制台,因此检索看起来正常

然后我呈现“回复”视图,它应该列出所有数据。这是“reples.ejs”中的相关示例

<body ng-app="myApp" ng-controller="contr">
  <h1>Overview of all the results:</h1>
  <br>
  <table>
    <th>Name:</th>
    <th>Gender:</th>
    <tr ng-repeat="reply in allreplies">
      <td> {{ reply.name }}</td>
      <td> {{ reply.gender }}</td>
    </tr>
  </table>
  <script>
    var app = angular.module('myApp', []);
    app.controller('contr', function($scope) {
      $scope.allreplies = allreplies;
    });
  </script>
</body>

所有结果概述:

姓名: 性别: {{reply.name} {{reply.gender} var-app=angular.module('myApp',[]); 应用控制器('控制',功能($范围){ $scope.allreplies=所有回复; });
似乎我没有正确地将对象传递到渲染视图。浏览器控制台状态为

ReferenceError:未定义所有答复


有人能看看我做错了什么吗?非常感谢

您需要像这样渲染回调内部的视图。异步函数接受异步进程完成后执行的回调

app.get('/viewreplies', function(req, res) {

  // allreplies contains the data from the MongoDB
  RepliesModel.find().then(function(result){
      console.log(result);
      res.render('replies', { allreplies : results });
  });

});
另外,您正在使用angular在服务器上处理数据,这是错误的

ng repeat=“重新应用于所有回复”

您需要使用ejs来处理数据并生成html。然后将响应发送给客户端。角度是从浏览器而不是从服务器使用的

在ejs模板中尝试类似的操作

<body>
  <h1>Overview of all the results:</h1>
  <br>
  <table>
    <th>Name:</th>
    <th>Gender:</th>
    <% for(var i=0; i<allreplies.length; i++) {%>
      <tr>
        <td><%= allreplies[i].name %></td>
        <td><%= allreplies[i].gender %></td>
      </tr>
    <% } %>
  </table>

</body>

所有结果概述:

姓名: 性别:
杰出的非常感谢。我太匆忙地开始使用angular(我喜欢HTML转换),并没有意识到我正在使用ejs。哎呀。现在一切都清楚了@艾尔弗雷德:如果有帮助的话,你能接受我的回答吗?