向Express Server发出多个AngularJS Post请求?服务器在第二个帖子请求时崩溃

时间:2017-12-12 21:52:24

标签: javascript angularjs node.js express angular-http

因为我无法在我的客户端同时运行这两个帖子请求,所以我试图在第一篇文章的.then部分中运行第二篇帖子。这对我的其他项目一直很好。但由于某些原因,当第二个发布请求触发时,我的服务器没有回复。当我检查服务器的控制台时,我注意到它崩溃了,并且出现了错误消息(在这篇帖子的底部)。

可能导致这种情况???

我在服务器代码的第二个帖子请求中添加了断点,并注意到断点甚至没有被击中。服务器在命中之前崩溃并给我提供继续选项。

客户端代码(当用户按下按钮时会被触发):

$scope.searchCharacter = function(){
    var request = {name: $scope.charName, realm: $scope.selectedRealm};
    //First post request
    $http.post('/searchCharacter', request)
    .then(function(response) {
        //sets some variables
        var id = 0;
        //Second post request
        $http.post('/helloworld', id)
        .then(function(response) {
            //sets some more variables      
            debugger;             
        });          
    });
}

服务器代码:

//First post request
app.post('/searchCharacter', jsonParser, function (req, res) {
    blizzard.wow.character(['profile', 'stats', 'items', 'statistics'], { origin: 'us', realm: req.body.realm.name, name: req.body.name })
    .then(response => {
        if(response.status != 200){
            res.send("That character doesn't exist! Please enter a valid character name.");
        } else {
            console.log(response.data);
            res.send(response.data);
        }
    });
});
//Second Post Request
app.post('/helloworld', jsonParser, function (req, res) {
    console.log(req.body);
    res.send("hello");
});

错误讯息:

  

SyntaxError:意外的令牌#

     

at Object.parse(native)

     

在createStrictSyntaxError   (C:\用户\ RDubz \文件\访谈\ EagleDream   17年12月7日\项目\ node_modules \体解析器\ lib中\类型\ json.js:157:10)

     

at parse(c:\ Users \ RDubz \ Documents \ Interviews \ EagleDream   17年12月7日\项目\ node_modules \体解析器\ lib中\类型\ json.js:83:15)

     

位于c:\ Users \ RDubz \ Documents \ Interviews \ EagleDream   17年12月7日\项目\ node_modules \体解析器\ lib中\ read.js:121:18

     

在invokeCallback(c:\ Users \ RDubz \ Documents \ Interviews \ EagleDream   17年12月7日\项目\ node_modules \体解析器\ node_modules \生体\ index.js:224:16)

     

完成后(c:\ Users \ RDubz \ Documents \ Interviews \ EagleDream   17年12月7日\项目\ node_modules \体解析器\ node_modules \生体\ index.js:213:7)

     

在IncomingMessage.onEnd   (C:\用户\ RDubz \文件\访谈\ EagleDream   17年12月7日\项目\ node_modules \体解析器\ node_modules \生体\ index.js:273:7)

     

在emitNone(events.js:67:13)

     

在IncomingMessage.emit(events.js:166:7)

     

at endReadableNT(_stream_readable.js:921:12)

2 个答案:

答案 0 :(得分:0)

Facepalm 我使用var id = 0并将其传递给我的函数,却没有意识到它需要作为对象或参数传递。感谢那些评论的人!

答案 1 :(得分:0)

试试这个:

$scope.searchCharacter = function(){
    var request = {name: $scope.charName, realm: $scope.selectedRealm};
    //First post request
    $http.post('/searchCharacter', request)
    .then(function(response) {
        //sets some variables
        var id = 0;
        //Second post request (append id to route).
        $http.post('/helloworld/' + id)
        .then(function(response) {
            //sets some more variables      
            debugger;             
        });          
    });
}

//First post request
app.post('/searchCharacter', jsonParser, function (req, res) {
    blizzard.wow.character(['profile', 'stats', 'items', 'statistics'], { origin: 'us', realm: req.body.realm.name, name: req.body.name })
    .then(response => {
        if(response.status != 200){
            res.send("That character doesn't exist! Please enter a valid character name.");
        } else {
            console.log(response.data);
            res.send(response.data);
        }
    });
});
//Second Post Request (get id from req.params.id)
app.post('/helloworld/:id', function (req, res) {
    console.log(req.params.id);
    res.send("hello");
});

它将id附加到helloworld请求,并使用helloworld/:id定义路由req.params.id以将ID从请求中提取出来。