在进行数据库调用时,如果控制器响应返回JSON对象,则无法在view.ejs中打印JSON对象值

我有通过控制器查询数据库的JSON响应,我想在view.ejs文件中打印这个JSON对象值。

view.ejs

<div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.username}}</td> <td>{{ x.password}}</td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("") .then(function (response) {$scope.names = response.users;}); }); </script> 

这是我的模型User.js

  module.exports = { autoCreatedAt: false, autoUpdatedAt: false, attributes: { username:{ type:'string', required: true, unique:true }, password:{ type:'string', required:true }, } }; 

这是我的控制器UserController.js

  module.exports = { get:function(req,res){ User.find().exec(function (err, users){ if (err) { return res.json(err); } return res.json(users); }); } }; 

我在下面的文件routes.jsconfiguration路由

  module.exports.routes = { '/': { view: 'view' }, '/user/get': 'UserController.get', }; 

当我点击URL“ http:// localhost:1337 / user / get ”时,我得到这下面的json响应,我想这个值被推入view.ejs文件。

 [ { "username": "root", "password": "root", "id": 1 }, { "username": "home", "password": "home", "id": 2 }, { "username": "unisys", "password": "unisys", "id": 3 } ] 

我想知道如果有人能帮助我了解我如何获得这些价值。感谢您的任何build议

response数据分配names的问题。 即

使用

$ scope.names = response。 数据用户; OR $ scope.names = response。 数据 ;

代替

$ scope.names = response.users;

作为回应本身你得到的对象数组,所以如果你直接分配给$ scope.names,它将工作。

如果它帮助你,只要让我知道。

$ http.get(“localhost:1337 / user / get”)。then(function(response){$ scope.names = response; console.log(response);})