2017-11-14 1 views
1

프론트 엔드에서 JSON 객체를 받고 엔드 포인트/getData로 백엔드로 보냅니다. 여기서는 API에서 데이터를 얻기 위해 GET 요청을 사용하고 마지막 POST 요청에서 JSON 객체와 GET 응답 본문을 모두 보내야합니다. 그러나 요청을 보내면 GET 응답 본문이 너무 늦게 나오고 "formInfo"는 정의되지 않습니다.POST 요청이 시작되기 전에 어떻게 GET 요청의 응답 본문 내용을 가져 옵니까? 약속대로 할 수 있습니까?

GET이 완료되면 POST 요청이 전송되도록 어떻게 수정합니까?

app.post('/getData', function(req, res) { 
    debugger; 
    var data = req.body; 
    console.log(data); 
    toSend = data; 
    res.send({msg: "Success"}); 
    var findID = {}; 
    var endPoint = 'https://secure.p01.eloqua.com/API/REST/2.0/assets/form/' + toSend["formID"].toString(); 
    var options = { 
     method: "GET", 
     headers: {'Authorization': authenticationHeader, 'Content-Type': 'application/json'} 
    }; 
    request.get(endPoint, options, function (error, response, body) { 
     console.log(body); 
     findID = body["elements"]; 
      request({ 
        method: "POST", 
        headers: {'content-type': 'application/json', 'authorization': authenticationHeader}, 
        url: 'http://localhost:3000/handleData', 
        json: { 
         "tuples": toSend, 
         "formInfo": body['elements'] 
        }}, 
       function (error, response, body) { 
        console.log(response); 
       }); 
    }); 
}); 

답변

0

fetch API을 사용해 보셨습니까?

최신 브라우저에서는 widely supported입니다. 최악의 경우 브라우저를 변형 할 수 있습니다. NodeJS.

이렇게하면 요청을 연결할 수 있습니다.

fetch(`first.url.com`) 
    .then(response => 
    fetch(`second.url.com`, {body: response.body}) 
) 
관련 문제