データベース呼び出しが行われたときにコントローラーからの応答がJSONオブジェクトを返す場合、view.ejsにJSONオブジェクト値を出力できません

user3534759

データベースクエリからコントローラーを介したJSON応答があり、このJSONオブジェクト値をview.ejsファイルに出力したいと思います。

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.jsでルートを設定しています

 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
 }
]

誰かが私がこれらの値を取得する方法を理解するのを手伝ってくれるかどうか知りたいです。提案をありがとう

vineet

データnamesから割り当てる際の問題responseすなわち、

使用する

$ scope.names =応答。data .users; または$ scope.names = response。データ;

の代わりに

$ scope.names = response.users;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ