Possibly unhandled rejection for HTTP request

2019-08-23 03:18发布

here I am unable to print my console code console.log("I got the data I requested"); in my node server

the error that is displayed in the browser console is in the image below

here is the browser console image

here is the code of server.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + "/public"))

app.listen(3000);
console.log("server running on port 3000")

here is the code of controller.js

var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
    console.log("Hello World from controller");
    $http({
        method: 'GET',
        url: '/employeelist'
    }).then(function (response) {
            console.log("I got the data I requested");
            $scope.employeelist = response.data;
        });
    employee1 = {
        name: 'Sunil',
        designation: 'Software Developer',
        salary: '20000'
    };
    employee2 = {
        name: 'Vamshi',
        designation: 'Java Developer',
        salary: '30000'
    };

    employee3 = {
        name: 'Chethan',
        designation: 'Dot Net Developer',
        salary: '20000'
    };

    var employeelist = [employee1, employee2, employee3];

    $scope.employeelist = employeelist;
}]);

I trying to print the console code in my node server

i am unable to print my console.log("") here

node server command prompt

like this i need it

2条回答
趁早两清
2楼-- · 2019-08-23 03:27

i have changed the server.js and controller.js codes and now if I open the localhost I can a receive a get request

here are the changes i have made

server.js

var express = require('express');
var app = express();

app.use(express.static(__dirname + "/public"));

app.get("/employeelist", function (req,res) {
    console.log("I received a GET Request")

    employee1 = {
        name: 'Sunil',
        designation: 'Software Developer',
        salary: '20000'
    };

    employee2 = {
        name: 'Vamshi',
        designation: 'Java Developer',
        salary: '30000'
    };

    employee3 = {
        name: 'Chethan',
        designation: 'Dot Net Developer',
        salary: '20000'
    };

    var employeelist = [employee1, employee2, employee3];
    res.json(employeelist);
});

app.listen(3000);
console.log("server running on port 3000");

controller.js

var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
    console.log("Hello World from controller");
    $http({
        method: 'GET',
        url: '/employeelist'
    }).then(function onSuccess(response) {
        console.log("data recieved");
        $scope.employeelist=response.data;
    },function onError(error) {
            console.log("Unknown error");
            $scope.employeelist=error.data;
        });
}]);

index.html

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
    <title>Employees List</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
    <h1>Employees List</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Designation</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="employee in employeelist">
                    <td>{{employee.name}}</td>
                    <td>{{employee.designation}}</td>
                    <td>{{employee.salary}}</td>
                </tr>
            </tbody>
        </table>
</div>
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/
angular.min.js"></script>
<script src="../../controllers/controller.js"></script>
</body>
</html>
查看更多
Rolldiameter
3楼-- · 2019-08-23 03:42

Try catching errors

$http({
    method: 'GET/POST',
    url: 'your/endpoint/url'
})
.then(function onSuccess(response) {
    console.log(response);
})
.catch(function onError(error) {
    console.log(error);         
});

If you want to suppress the warnings globally

app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

but it is always recommended to catch errors, not to suppress warnings

查看更多
登录 后发表回答