繁体   English   中英

连接到api nodeJS和mongoDB

[英]Connecting to a api nodeJS and mongoDB

我正在尝试将我的网络应用程序连接到mongoose的后端服务器。 此服务器在本地atm托管。

我正试图从我的服务器获取一个“食谱”对象并获得它的标题。 我收到以下错误:

angular-ui-router.js:18 Uncaught TypeError: Cannot read property 'isDefined' of undefined(anonymous function) @ angular-ui-router.js:18(anonymous function) @ angular-ui-router.js:3223 angularApp.js:3 Uncaught ReferenceError: angular is not defined

我正在使用Post Man获取我的对象。

[
 {
  "_id": "56309ea8e4b02bf207dbe409",
   "title": "Chili sin carne",
 }
]

我们的HTML页面

<html>
<head>
<title>Recipes</title>
<link rel='stylesheet' href='/stylesheets/inlog.css' />
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-               router/0.2.10/angular-ui-router.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">         </script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
<script src="javascripts/angularApp.js"></script>
</head>
<body>
<table >
  <div ng-controller="RecipesCtrl">
 <h1>Title </h1>
//here we load a recipe object and get it's title and show it
  <p>{{$scope.title}}</p>
</div>
</body>
</html>

使用Javascript

'use strict';

var app = angular.module('project-eva', []);

app.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider,$urlRouterProvider){
$stateProvider
.state('home', {
  url: '/home', 
  templateUrl: '/home.html',
  controller: 'MainCtrl',

})
.state('login', {
  url: '/login',
  templateUrl: '/login.html',
  controller: 'loginCtrl',
  onEnter: ['$state', 'auth', function($state, auth){
    if(auth.isLoggedIn()){
      $state.go('home');
    }
  }]
})
.state('register', {
  url: '/register',
  templateUrl: '/register.html',
  controller: 'AuthCtrl',
  onEnter: ['$state', 'auth', function($state, auth){
    if(auth.isLoggedIn()){
      $state.go('home');
    }
  }]
})
.state('recipes', {
  url: '/recipes',
  templateUrl: '/recipes.html',
  controller: 'RecipeCtrl',
  onEnter: ['$state', 'auth', function($state, auth){
    if(auth.isLoggedIn()){
      $state.go('home');
    }
  }]
  });
  $urlRouterProvider.otherwise('home');
  }]);




app.factory('recipes', ['$http' ,function($http){
var o = {
    recipes: []
};
o.getAll = function(){
    return $http.get('/recipes').success(function(data){
        angular.copy(data,o.recipes);

    });
};
return o;
}]);

配方控制器

var app = angular.module('project-eva')

 app.controller('RecipesCtrl', function ($scope, $location, $rootScope,     $http) {
"use strict";


$scope.recipes = function(){

    $http({
      method: 'GET',
      url: 'localhost:8080/api/recipes',
      headers: {
        'Access-Control-Allow-Origin' : '*',
        'Content-Type': 'application/json'
      },
      data: {
        title : $scope.title;
      }
    }).then(function succesCallBack(response){
      console.log(response);

    }, function errorCallback(response){
         console.log(response);
         if(response.data !== null){
           $scope.error = response.data.message;
         } else {
           $location.path('/ise');
         }
    });
  } else {
    $scope.error = "Please fill in all required fields.";
  }
} else {
  $scope.error = "Password and confirm password have to be te same!";
}
}
});

你在html中导入了两次angular-ui-router,但是从来没有角度。 从angular.io下载它。 你也可以使用bower或npm。 你必须有一个

<script src="somewhere/angular.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM