我无法获取我的get请求,我使用angular和im new to it并使用它,因为我的项目决定我必须使用它,我试图从我的mongodb batabase获取我的条目,我也在运行快递服务器附上所有代码,我可以告诉我我的错误,我确定有很多。
wcs_is_in_trial
请告诉我,如果我需要发布更多文件/代码以便清楚,我真的很抱歉这可能是格式不合适但我是初学者而且我正在尽我所能来设置它教程和codeacademy,我只需要能够显示我的数据以及如何以相同的格式创建帖子。 我非常感谢任何想要帮助的人的帮助,请不要对菜鸟苛刻。
//factory
app.factory('whipmeet', ['$http', function($http) {
return $http.get('http://mongodb://127.0.0.1:27017/finalwhipmeet')
.success(function(data) {
return JSON.parse(data);
})
.error(function(err) {
return err;
});
}]);
//controller
app.controller('MainController', ['$scope', 'whipmeet', function($scope, whipmeet) {
whipmeet.success(function(data) {
$scope.meetinfo = data,
$scope.meetlikes = 23;
});
}]);
//index.html(only view)
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
</head>
<body ng-app="WhipMeetApp">
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<h1 id="test1"> teting </h1>
<div class="main" ng-controller="MainController">
<div class="container">
<div class="card" ng-repeat="meet in meets">
<meet-info info="likes"></meet-info>
</div>
</div>
</div>
<!-- modules -->
<script src="/javascripts/app.js"></script>
<!-- controllers -->
<script src="/javascripts/controllers/MainController.js"></script>
<!-- services -->
<script src="/javascripts/services/whipmeet.js"></script>
<!-- directives -->
<script src="/javascripts/directives/meetInfo.js"></script>
</body>
</html>
//schema im trying to pull
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var MeetSchema = new Schema ({
name: String,
location: String,
car: String,
date: Date,
time: Number,
type: String,
stock: Boolean
});
module.exports = mongoose.model('Meet', MeetSchema);
//error i get in console
angular.js:9734 GET http://mongodb//127.0.0.1:27017/finalwhipmeet net::ERR_NAME_NOT_RESOLVED
答案 0 :(得分:0)
该示例使用回调成功和错误函数。你也可以使用promises。
角度工厂调用快速路由,它与mongoDB连接以进行创建,更新,删除或获取操作,并将响应发送回工厂。将结果/错误传递给控制器。
app.controller('MainController', ['$scope', 'whipmeet', function($scope, whipmeet) {
//get all
whipmeet.getWhipmeet(function(data){
$scope.meetinfo = data;
},
function(err){
console.log(err)
});
//get one
whipmeet.getWhipmeet("id::1234567", function(data){
},
function(err){
console.log(err)
});
//create
whipmeet.createWhipmeet({key: "some value"}, function(data){
},
function(err){
console.log(err)
});
}]);
app.factory('whipmeet', ['$http', function($http) {
return {
createWhipmeet: function (obj, success, error) {
$http.post('/meets/', obj, {}).
then(function (data) {
success(data)
}, function (e) {
error(e);
});
},
getWhipmeets: function (success, error) {
$http.get('/meets/').
then(function (data) {
success(data)
}, function (e) {
error(e);
});
},
getWhipmeet: function (id, success, error) {
$http.get('/meets/'+id).
then(function (data) {
success(data)
}, function (e) {
error(e);
});
}
}
}]);