2017-12-12 9 views
1

클라이언트에서이 두 게시물 요청을 동시에 실행할 수 없으므로 첫 번째 게시물의 .then 섹션에서 두 번째 게시를 실행하려고합니다. 어느 것이 항상 다른 프로젝트에서도 잘 작동했습니다. 하지만 두 번째 게시물 요청이 발생하면 내 서버가 응답하지 않는 몇 가지 이유가 있습니다. 서버의 콘솔을 확인할 때 충돌이 발생하고 오류 메시지가 표시됩니다 (이 게시물 하단).Express Server에 여러 AngularJS Post 요청을 만드시겠습니까? 두 번째 게시물 요청시 서버가 중단됨

무엇이이 원인 일 수 있습니까 ???

내 서버 코드에서 두 번째 게시 요청에 중단 점을 넣었으며 중단 점이 공격에 맞지 않는 것으로 나타났습니다. 서버가 충돌하기 전에 충돌하고 계속할 수있는 옵션을 제공합니다.

$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: 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)

+1

숫자가 'id = 0'인 경우 ID가 질식 할 수 있습니다. 디버깅을 위해서'id = {}'를 설정하거나'app.post ('/ helloworld')'에서'jsonParser'를 제거하고 충돌이 발생하는지 확인하십시오. –

+1

'helloworld' 경로의 경우 json 본문 대신 route param을 사용해야한다고 생각합니다. 즉,'app.post ('helloworld/: id ')'입니다. –

+1

app.post ('helloworld/: id')를 사용하는 경우 req.params.id를 사용하십시오. – AngelSalazar

답변

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

그것은 helloworld 요청에 id을 추가하고, 요청에서 ID를 끌어 req.params.id를 사용하여 경로 helloworld/:id을 정의합니다.

0

마른 세수 내가 VAR를 사용했다 id = 0 그리고 그것을 실현하지 않고 내 함수에 전달하는 것은 p 일 필요가 있었다. 객체 또는 param 중 하나로 처리됩니다. 댓글을 달았습니다.

+0

다행 이군. –