连接到 api nodeJS 和 mongoDB
Posted
技术标签:
【中文标题】连接到 api nodeJS 和 mongoDB【英文标题】:Connecting to a api nodeJS and mongoDB 【发布时间】:2016-02-04 06:17:22 【问题描述】:我正在尝试将我的网络应用程序连接到我在猫鼬中的后端服务器。此服务器托管在本地 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!";
);
【问题讨论】:
【参考方案1】:您在 html 中导入了 angular-ui-router 两次,但从不导入 angular 本身。从 angular.io 下载。你也可以使用 bower 或 npm。你必须有一个
<script src="somewhere/angular.js"></script>
【讨论】:
以上是关于连接到 api nodeJS 和 mongoDB的主要内容,如果未能解决你的问题,请参考以下文章
如何在NodeJS中代理websocket连接到其他websockets
无法连接到 Nodejs EC2 服务器上的 SQL Server 数据库
如何从 Angular Nginx 容器连接到 Node API docker 容器
Nginx 错误失败(111:连接被拒绝)同时连接到上游,docker-compose nodejs