简体   繁体   中英

Connecting to a api nodeJS and mongoDB

I'm trying to connect my web app to my backend server in mongoose. This server is hosted locally atm.

I'm trying to get an "recipe" object from my server and get its title. I'm getting following errors:

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

I'm getting my object using Post Man.

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

Our HTML page

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

Recipe Controller

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!";
}
}
});

You are importing angular-ui-router twice in the html, but never angular itself. Download it from angular.io. You could also use bower or npm. You must have a

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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