简体   繁体   中英

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. 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)

at 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)

at 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)

at done (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)

at emitNone (events.js:67:13)

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

at endReadableNT (_stream_readable.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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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