2017-05-05 92 views
1

所以我得到這個錯誤:「GET http://localhost:8000/blogs/undefined net :: ERR_EMPTY_RESPONSE angular.js:12587」。可能的控制器或至少執行兩次的函數

它發生在我進入我的主博客頁面以及當我進入博客分類頁面時。查看網絡標籤我得到: 1. blog-home.html 200 xhr angular.js 12587 4.6kb 21ms 2.所有200 xhr angular.js 12587 3.0kb 57ms 3.未定義待處理xhr angular.js 12587 3.0kb 57ms 0kb

和我去博客分類頁面時一樣。最終他們失敗並踢出上述錯誤。

因此,在此之前被拒絕...我已閱讀並嘗試在此堆棧: 沒有運氣。也許我錯過了某些東西或輸入了錯誤的東西。我完全傻眼了,我覺得這顯然超出了我的技能水平。因此,這裏是一串代碼... Server.js

var express = require('express'); 
 
var favicon = require('serve-favicon'); 
 
var bodyParser = require('body-parser'); 
 
var Mailgun = require('mailgun-js'); 
 
var path = require('path'); 
 
require('colors'); 
 

 
var app = express(); 
 

 

 
app.use(favicon(path.join(__dirname, 'client/assets/images', '*****'))) 
 

 
app.use(bodyParser.urlencoded({extended:true})); 
 
app.use(bodyParser.json({extended:true})) 
 

 
app.use(express.static(path.join(__dirname, 'client'))); 
 
app.use(express.static("./bower_components")); 
 

 
//app.all('/*', function(req, res, next) { \t \t \t  
 
// res.sendFile('index.html', { root: 'client'}); 
 
//}); 
 

 
require('./server/config/mongoose.js'); 
 

 
var routes = require('./server/config/routes.js'); 
 
routes(app); 
 

 
var api_key = '*********'; 
 
var domain = '*********'; 
 
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain}); 
 
    
 
var data = { 
 
    from: '********', 
 
    to: '********', 
 
    subject: 'Hello', 
 
    text: 'Testing some Mailgun awesomness!' 
 
}; 
 
    
 
mailgun.messages().send(data, function (error, body) { 
 
    console.log(body); 
 
}); 
 

 
app.listen(8000, function() { 
 
    console.log("I'm listening...".blue); 
 
})

的Index.html

<!DOCTYPE html> 
 
<html lang="en" ng-app="Bridgeman"> 
 
<head> 
 

 
    <meta charset="utf-8"> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
 
    <meta name="description" content=""> 
 
    <meta name="author" content=""> 
 

 
    <meta name="description" content=""> 
 
    <meta name="author" content=""> 
 
    <meta name="revisit-after" content="10 days"> 
 
    <meta name="googlebot" content="noodp"> 
 
    <meta name="msnbot" content="noodp"> 
 
    <meta name="slurp" content="noodp, noydir"> 
 
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' ; connect-src 'self'; img-src * data:; style-src 'self' 'unsafe-inline'; font-src * data:; frame-src https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d44591.890384371676!2d-118.36723983279781!3d33.83006153459027!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1sen!2sus!4v1492366398808 "> 
 
    
 
    
 
    <meta content="/assets/images/******_BigBearSnowTrail.jpg" itemprop="image"> 
 
    <link href="/assests/images/*******.ico" rel="shortcut icon"> 
 

 
    <base href="/" /> 
 

 
    <title></title> 
 
    
 
    <link href="assets/css/bootstrap.min.css" rel="stylesheet"> 
 
    <link href="assets/css/modern-business.css" rel="stylesheet"> 
 
    <link href="assets/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> 
 

 
    <script type="text/javascript" src="angular/angular.js"></script> 
 
    <script type="text/javascript" src="angular-route/angular-route.js"></script> 
 
    <script src="app.js"></script> 
 

 
    <script src="/factories/ContactFactory.js"></script> 
 
    <script src="/factories/loginFactory.js"></script> 
 
    <script src="/factories/blogFactory.js"></script> 
 

 
    <script src="/controllers/contactController.js"></script> 
 
    <script src="/controllers/servicesController.js"></script> 
 
    <script src="/controllers/blogController.js"></script> 
 
    <script src="/controllers/blogViewController.js"></script> 
 
    <script src="/controllers/blogAWSController.js"></script> 
 
    <script src="/controllers/loginController.js"></script> 
 
    
 
    <base target="_blank"> 
 
</head> 
 

 
<body> 
 

 
    <div ng-include='"templates/header.html"'></div> 
 
    <div ng-view=""></div> 
 
    <div ng-include='"templates/footer.html"'></div> 
 

 
    
 
    
 
    <script src="assets/js/jquery.js"></script>  
 
    <script src="assets/js/bootstrap.min.js"></script>  
 
</body> 
 
</html>

app.js(正如你看到的我試着將AWS類別分離到它自己的控制器中,並且當頁面加載時都會被解僱)

var Bridgeman = angular.module('Bridgeman', ['ngRoute']); 
 

 
\t Bridgeman.config(['$routeProvider', '$httpProvider', '$locationProvider', '$qProvider', function($routeProvider, $httpProvider, $locationProvider, $qProvider){ 
 
\t \t \t $qProvider.errorOnUnhandledRejections(false); 
 
\t \t \t $httpProvider.interceptors.push(function($q, $location){ \t \t \t \t \t \t \t \t \t 
 
\t \t \t \t return{ 
 
\t \t \t \t \t 'responseError': function(rejection){ 
 
\t \t \t \t \t \t if(rejection.status == 401){ 
 
\t \t \t \t \t \t \t $location.url('/'); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t \t return $q.reject(rejection); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t } 
 
\t \t \t \t }); 
 
\t \t \t $routeProvider 
 
\t \t \t \t .when('/', { 
 
\t \t \t \t \t templateUrl:'partials/home.html', \t \t \t \t \t 
 
\t \t \t \t }).when('/about', { 
 
\t \t \t \t \t templateUrl:'partials/about.html', \t \t \t \t \t 
 
\t \t \t \t }).when('/services', { 
 
\t \t \t \t \t templateUrl: 'partials/services.html', 
 
\t \t \t \t \t controller: 'servicesController' 
 
\t \t \t \t }).when('/blog', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-home.html', 
 
\t \t \t \t \t controller: 'blogViewController' 
 
\t \t \t \t }).when('/blogAWS', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-aws.html', 
 
\t \t \t \t \t controller: 'blogAWSController' 
 
\t \t \t \t }).when('/blogSEO', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-SEO.html', 
 
\t \t \t \t \t controller: 'blogViewController' 
 
\t \t \t \t }).when('/blogSecurity', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-Security.html', 
 
\t \t \t \t \t controller: 'blogViewController' 
 
\t \t \t \t }).when('/blogBusiness', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-Business.html', 
 
\t \t \t \t \t controller: 'blogViewController' 
 
\t \t \t \t }).when('/blogSportsTech', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-SportsTech.html', 
 
\t \t \t \t \t controller: 'blogViewController' 
 
\t \t \t \t }).when('/blog-post/:id', { 
 
\t \t \t \t \t templateUrl: 'partials/blog-post.html', 
 
\t \t \t \t \t controller: 'blogViewController' \t \t \t \t \t 
 
\t \t \t \t }).when('/contact', { 
 
\t \t \t \t \t templateUrl: 'partials/contact.html', 
 
\t \t \t \t \t controller: 'contactController' 
 
\t \t \t \t }).when('/login', { 
 
\t \t \t \t \t templateUrl:'partials/login.html', 
 
\t \t \t \t \t controller: 'loginController' 
 
\t \t \t \t }).when('/admin', { 
 
\t \t \t \t \t templateUrl:'partials/admin.html', 
 
\t \t \t \t \t controller: 'blogController' 
 
\t \t \t \t }).otherwise({ 
 
\t \t \t \t \t redirectTo:'/' 
 
\t \t \t \t }); 
 
\t \t \t $locationProvider.html5Mode(true); 
 
\t \t }]) 
 

 
\t Bridgeman.run(function($rootScope, $location, $anchorScroll, $routeParams) { 
 
    \t \t $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) { 
 
    \t \t $location.hash($routeParams.scrollTo); 
 
    \t \t $anchorScroll(); 
 
    }); 
 
})

博客-home.html的(這是主要的博客頁面的時候觸發關在我的blogViewController兩種功能)

<!-- Page Content --> 
 
    <div class="container"> 
 

 
     <!-- Page Heading/Breadcrumbs --> 
 
     <div class="row"> 
 
      <div class="col-lg-12"> 
 
       <h1 class="page-header">Blog Home 
 
        <small>My insights just for you</small> 
 
       </h1> 
 
       <ol class="breadcrumb"> 
 
        <li><a href="/blog">Home</a> 
 
        </li> 
 
        <li class="active">Blog Home</li> 
 
       </ol> 
 
      </div> 
 
     </div> 
 
     <!-- /.row --> 
 

 
     <div class="row"> 
 

 
      <!-- Blog Entries Column --> 
 
      <div class="col-md-8"> 
 
       <div class="blogs" ng-repeat="b in blogs | orderBy:'-createdAt' | filter:searchblogs"> 
 

 
       <!-- Blog Post --> 
 
       <h2> 
 
        <p>{{b.title}}</p> 
 
       </h2> 
 
       <p class="lead"> 
 
        by {{b._user.name}} in the {{b.category}} category 
 
       </p> 
 
       <p><i class="fa fa-clock-o"></i> Posted on {{b.createdAt | date: "MMM. dd, yyyy"}}</p> 
 
       <hr> 
 
       <p>{{b.snippet}}</p> 
 
       <hr> 
 
       <p>{{b.content}}</p>     
 
       <a class="btn btn-primary" ng-href="/blog-post/{{b._id}}">Read More <i class="fa fa-angle-right"></i></a> 
 

 
       <hr> 
 
       </div> 
 
       <hr> 
 
       </div> 
 

 
      <!-- Blog Sidebar Widgets Column --> 
 
      <div class="col-md-4"> 
 

 
       <!-- Blog Search Well --> 
 
       <div class="well"> 
 
        <h4>Search my blogs</h4> 
 
        <div class="input-group"> 
 
         <input type="text" class="form-control" ng-model="searchblogs"> 
 
         <span class="input-group-btn"> 
 
          <button class="btn btn-default" type="button"><i class="fa fa-search"></i></button> 
 
         </span> 
 
        </div> 
 
        <!-- /.input-group --> 
 
       </div> 
 

 
       <!-- Blog Categories Well --> 
 
       <div class="well"> 
 
        <h4>Blog Categories</h4> 
 
        <div class="row"> 
 
         <div class="col-lg-6"> 
 
          <ul class="list-unstyled"> 
 
           <li><a href="/blogAWS">AWS</a> 
 
           </li> 
 
           <li><a href="/blogSEO">SEO</a> 
 
           </li> 
 
           <li><a href="/blogSecurity">Security</a> 
 
           </li> 
 
           <li><a href="/blogBusiness">Business Tips</a> 
 
           </li> 
 
          </ul> 
 
         </div> 
 
         <!-- /.col-lg-6 --> 
 
         <div class="col-lg-6"> 
 
          <ul class="list-unstyled"> 
 
           <li><a href="/blogSportsTech">Tech in Sports</a> 
 
           </li> 
 
           <li><a href="#">Coming Soon</a> 
 
           </li> 
 
           <li><a href="#">Coming Soon</a> 
 
           </li> 
 
           <li><a href="#">Coming Soon</a> 
 
           </li> 
 
          </ul> 
 
         </div> 
 
         <!-- /.col-lg-6 --> 
 
        </div> 
 
        <!-- /.row --> 
 
       </div> 
 

 
       <!-- Side Widget Well --> 
 
       <div class="well"> 
 
        <h4>My Blog</h4> 
 
        <p>Here is a collection of my genius (ha!) and tech insights. I'm sure sooner than later I will discuss Formula 1 tech but be assured there will be plenty of SEO and various web development tips/tools/tricks. And lets not forget some general business savviness I've learned over the years.</p> 
 
       </div> 
 
      </div> 
 
     </div> 
 
     <!-- /.row -->

blogViewController(其中函數是。再一次,我評論了getOneBlog並且錯誤沒有發生。那麼,什麼是引發這場火災,並單擊該按鈕之前?)

Bridgeman.controller('blogViewController', function($scope, blogFactory, $location, $routeParams){ 
 
    console.log("in the blog VIEW controller"); 
 

 
    $scope.blogs = []; 
 
    $scope.one = []; 
 

 
    blogFactory.getAllBlogs(function(output){ 
 
    $scope.blogs = output; 
 
    console.log(output); 
 
    }) 
 

 
    blogFactory.getOneBlog($routeParams.id, function(output){ 
 
    $scope.one = output; 
 
    console.log(output); 
 
    }) 
 
});

blogFactory(這裏沒有任何問題,我知道的)

Bridgeman.factory('blogFactory', function($http){ 
 
    var factory = {}; 
 

 
    factory.submitNewBlog = function(input, callback){ 
 
    $http.post('/blogs/new', input).then(function(output){ 
 
     console.log("we just added a new blog"); 
 
     callback(output.data); 
 
    }); 
 
    } 
 
    //factory.submitNewComment = function(input, callback){ 
 
    // $http.post('/comments/new', input).then(function(output){ 
 
    // console.log("we just added a new comment"); 
 
    // callback(output.data); 
 
    // }); 
 
    //} 
 

 
    factory.getAllBlogs = function(callback){ 
 
    $http.get('/blogs/all').then(function(output){ 
 
     console.log("we just got all blogs"); 
 
     callback(output.data); 
 
    }); 
 
    } 
 

 
    factory.getOneBlog = function(blogID, callback){   //factory.getOneBlog = function(blogID, callback) 
 
    $http.get('/blogs/' + blogID).then(function (output){   //$http.get('blogs/' + blogID).then(function (output){ 
 
     console.log(output.data);        //callback(output.data); 
 
     console.log("we just got one blog"); 
 
     callback(output.data); 
 
    }); 
 
    } 
 

 
    return factory; 
 
});

路線(這裏有幾種不同的風格只是搭配不同的格式來解決這個問題)

var users = require('./../controllers/users.js'); 
 
var blogs = require('./../controllers/blogs.js'); 
 

 
module.exports = function(app){ 
 
\t 
 
\t app.post('/reg', function(req, res){ 
 
\t users.reg(req, res); 
 
\t }); 
 

 
\t app.post('/login', function(req, res){ 
 
\t users.login(req, res); 
 
\t }); 
 
\t 
 
\t app.get('/blogs/all', function(req, res) { 
 
     blogs.getAllBlogs(req, res); 
 
    }); 
 

 
    app.get('/blogs/:id', blogs.getOneBlog); \t \t \t //app.get('/blogs/:id', blogs.getOneBlog)  
 
    
 

 
    app.post('/blogs/new', function(req, res) { 
 
     blogs.addBlog(req, res); 
 
    }); 
 

 
\t app.post('/contact', function(req,res){ 
 

 
\t \t var api_key = 'key-2451a2b90a87be616ab68b8f7c8f97ea'; 
 
\t \t var domain = 'sandbox7dedeb0d5d384b6a8ce4f49165204257.mailgun.org'; 
 
\t \t var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain}); 
 
\t \t 
 
\t \t var data = { 
 
\t \t from: 'Website inquiry <[email protected]>', 
 
\t \t to: '*************com', 
 
\t \t subject: req.body.full_name+" has sent you a message", 
 
\t \t html: 
 
\t \t \t req.body.full_name+" ..."+ \t \t \t 
 
\t \t \t req.body.phone+" ..."+ 
 
\t \t \t req.body.email+" ..."+ \t \t \t \t \t 
 
\t \t \t req.body.message 
 
\t \t }; 
 
\t \t 
 
\t \t mailgun.messages().send(data, function (error, body) { 
 
\t \t console.log(body); 
 
\t \t console.log("working..."); 
 
\t \t if(!error) 
 
\t \t  res.send("Your message has been sent"); 
 
\t \t else 
 
\t \t \t res.send("Uh oh... something went wrong!"); 
 
\t \t }); 
 
\t }); 
 
}

blogs.js服務器端控制器(在這裏沒有問題...我所知道的)

var Blog = mongoose.model('Blog'); 
 

 
module.exports = (function() { 
 
    return { 
 
     getAllBlogs: function(req, res){ 
 
     Blog.find({}).populate([{path : '_user'}]).exec(function(err, b){ 
 
      if(err){ 
 
      console.log("there was an error when getting all blogs".red); 
 
      } else { 
 
      console.log(b); 
 
      console.log("successfully got all blogs".green); 
 
      res.json(b); 
 
      } 
 
     }); 
 
     }, 
 

 
     getOneBlog: function(req, res) { 
 
     console.log('Rich band aid'); 
 
     if (req.params.id !== 'undefined') { 
 
     console.log('there was an id', req.params.id); 
 
     Blog.findOne({_id: req.params.id}).exec(function(err, b) {  //({_id: req.params.id}, function(err,b){}) 
 
      if(err){ 
 
      console.log('error is', err); 
 
      console.log("there was an error when getting the blog".red); 
 
      } else { 
 
      console.log(b); 
 
      console.log("successfully got the blog".green); 
 
      res.json(b); 
 
      } 
 
     }); 
 
     }else { 
 
     console.log('no id'); 
 
     } 
 
     }, 
 

 
     addBlog: function(req, res) { 
 
     console.log("===========================".yellow); 
 
     console.log(req.body); 
 
     console.log("===========================".yellow); 
 

 
     var b = new Blog({category: req.body.category, title: req.body.title, snippet: req.body.snippet, content: req.body.content, _user: req.body._user}) 
 
     b.save(function(err){ 
 
      if(err){ 
 
      console.log("there was an error when saving a blog".red); 
 
      } else { 
 
      console.log(b); 
 
      console.log("successfully saved the above blog".green); 
 

 
      res.redirect('/blogs/all'); 
 
      } 
 
     }) 
 
     } 
 
    } 
 
})();

和貓鼬分貝(在這裏沒有問題......我是知道的)

var mongoose = require('mongoose'); 
 

 
// Create the message schema 
 
var BlogSchema = new mongoose.Schema({ 
 
\t category: {type: String, required: true, minlength: 3, enum:['Security', 'Business', 'SEO', 'AWS', 'Tech in Sports']}, 
 
\t title: {type: String, required: true, minlength: 3}, 
 
\t snippet: {type: String, required: true, minlength: 3}, 
 
    content: {type: String, required: true, minlength: 3}, 
 
    _user: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}  
 
}, {timestamps: true}); 
 

 
mongoose.model('Blog', BlogSchema);

任何非常感謝幫助。謝謝

PS ...是的我知道你嘗試刷新頁面時frame-src和font-src的CPS錯誤。超級討厭。

+0

請檢查你的角度路徑,方向 – RAj

+0

@RAj感謝。我添加了server.js以及希望你能詳細說明你的意思?非常感謝您花時間和這個noob out。 – LANole

+0

@RAj所以我一直在玩角文字標籤,唯一讓我有所不同的是當我將角度和角度路線腳本標籤移動到索引頁的底部時。當我這樣做時,我可以去我的博客主頁,只是在頁面本身和getAllBlogs被解僱。 getOneBlog沒有啓動,也沒有被掛起。不幸的是,沒有任何博客在頁面上呈現。 – LANole

回答

0

能夠找出一個朋友。問題通過將2個函數包裝到'if'語句中解決。這可能不是正確的解決方案,但它完美地工作。

Bridgeman.controller('blogViewController', function($scope, blogFactory, $location, $routeParams){ 
 
    console.log("in the blog VIEW controller"); 
 

 
    $scope.blogs = []; 
 
    $scope.one = []; 
 

 

 
    if ($routeParams.id) { 
 
    blogFactory.getOneBlog($routeParams.id, function(output) { 
 
    $scope.one = output; 
 
    console.log(output); 
 
    }); 
 
    } else { 
 
    blogFactory.getAllBlogs(function(output) { 
 
    $scope.blogs = output; 
 
    console.log(output); 
 
    }); 
 
    } 
 
});