简体   繁体   English

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

[英]Making multiple AngularJS Post requests to an Express Server? Server crashes on second post request

So given that I can't run these two post requests at the same time in my client, I'm trying to run the second post in the .then section of the first post. 因此,鉴于我无法同时在客户端中运行这两个发布请求,因此我尝试在第一篇文章的.then部分中运行第二篇文章。 Which has always worked fine on my other projects. 在我的其他项目上,它一直运行良好。 But for some reason when the second post request fires my server doesn't reply. 但是由于某些原因,当第二个发布请求触发时,我的服务器没有回复。 When I check the console of the server, I notice it crashed and there's an error message (at the bottom of this post). 当我检查服务器的控制台时,我注意到它崩溃了,并且出现了一条错误消息(在本文的底部)。

What could be causing this??? 是什么原因造成的???

I have put breakpoints on the second post request in my server's code, and noticed the breakpoints don't even get hit. 我在服务器代码中的第二个发布请求上添加了断点,并注意到断点甚至没有被击中。 The server crashes before hitting and giving me the option to continue. 服务器在命中之前崩溃,并让我选择继续。

Client Code (gets fired when user presses a button): 客户端代码(当用户按下按钮时触发):

$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;             
        });          
    });
}

Server Code: 服务器代码:

//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");
});

Error message: 错误信息:

SyntaxError: Unexpected token # 语法错误:意外的令牌#

at Object.parse (native) 在Object.parse(本机)

at createStrictSyntaxError (c:\\Users\\RDubz\\Documents\\Interviews\\EagleDream 12-7-17\\Project\\node_modules\\body-parser\\lib\\types\\json.js:157:10) 在createStrictSyntaxError(c:\\ Users \\ RDubz \\ Documents \\ Interviews \\ EagleDream 12-7-17 \\ Project \\ node_modules \\ body-parser \\ lib \\ types \\ json.js:157:10)

at parse (c:\\Users\\RDubz\\Documents\\Interviews\\EagleDream 12-7-17\\Project\\node_modules\\body-parser\\lib\\types\\json.js:83:15) 在解析时(c:\\ Users \\ RDubz \\ Documents \\ Interviews \\ EagleDream 12-7-17 \\ Project \\ node_modules \\ body-parser \\ lib \\ types \\ json.js:83:15)

at c:\\Users\\RDubz\\Documents\\Interviews\\EagleDream 12-7-17\\Project\\node_modules\\body-parser\\lib\\read.js:121:18 在c:\\ Users \\ RDubz \\ Documents \\ Interviews \\ EagleDream 12-7-17 \\ Project \\ node_modules \\ body-parser \\ lib \\ read.js:121:18

at invokeCallback (c:\\Users\\RDubz\\Documents\\Interviews\\EagleDream 12-7-17\\Project\\node_modules\\body-parser\\node_modules\\raw-body\\index.js:224:16) 在invokeCallback(c:\\ Users \\ RDubz \\ Documents \\ Interviews \\ EagleDream 12-7-17 \\ Project \\ node_modules \\ body-parser \\ node_modules \\ raw-body \\ index.js:224:16)

at done (c:\\Users\\RDubz\\Documents\\Interviews\\EagleDream 12-7-17\\Project\\node_modules\\body-parser\\node_modules\\raw-body\\index.js:213:7) 完成时(c:\\ Users \\ RDubz \\ Documents \\ Interviews \\ EagleDream 12-7-17 \\ Project \\ node_modules \\ body-parser \\ node_modules \\ raw-body \\ index.js:213:7)

at IncomingMessage.onEnd (c:\\Users\\RDubz\\Documents\\Interviews\\EagleDream 12-7-17\\Project\\node_modules\\body-parser\\node_modules\\raw-body\\index.js:273:7) 在IncomingMessage.onEnd(c:\\ Users \\ RDubz \\ Documents \\ Interviews \\ EagleDream 12-7-17 \\ Project \\ node_modules \\ body-parser \\ node_modules \\ raw-body \\ index.js:273:7)

at emitNone (events.js:67:13) 在emitNone(events.js:67:13)

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

at endReadableNT (_stream_readable.js:921:12) 在endReadableNT(_stream_visible.js:921:12)

Facepalm I was using var id = 0 and passing it to my function without realizing it needed to be passed as either an object or a param. Facepalm我正在使用var id = 0并将其传递给我的函数,而没有意识到需要将其作为对象或参数传递。 Thanks to those who commented! 感谢那些发表评论的人!

Try this: 尝试这个:

$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");
});

It appends id to the helloworld request, and defines a route helloworld/:id using req.params.id to pull the id out of the request. 它将id附加到helloworld请求中,并使用req.params.id定义路由helloworld/:id将该ID移出请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM