2017-10-30 1 views
-1

좋아요, 그렇기 때문에 이것이 필요한 것보다 더 사소한 것 같지만 어떤 이유로 get 메소드에 전달 될 때 내 "본문"에서 정보를 가져올 수 없습니다. 나는 그것에 접근하기 위해 여러 가지 다른 방법을 시도했지만, 그 트릭을 보는 것은 아무 것도 없다. 안내가 대단히 감사합니다. 당신은 req.query.phone2req.query.email2를 사용할 필요가가져 오기 방법의 가져 오기에서 데이터에 액세스하려면 어떻게해야합니까?

---------------fetch method---------------- 
    var x = e.target.phone.value; 
    var y = e.target.email.value; 

    console.log(x); // proper output 
    console.log(y); // proper output 

    fetch('/api/insert', 
    { 
    accept: 'application/json', 
    body: { 
     phone2: x, 
     email2: y 
    } 
    }); 
------------get method-------------- 
app.get('/api/insert', (req, res) => { 
    const phone = req.body.phone2; <-- Am I accessing the data incorrectly? 

    const email = req.body.email2; <-- Am I accessing the data incorrectly? 
    console.log(phone); // getting undefined here... Why? 
    console.log(email); // and here... 
    }); 
    }); 
+0

'을 console.log (REQ)'와'을 console.log (고해상도)'의 출력은 무엇인가? – gurvinder372

답변

2

: 다음은 내 코드입니다.

URL에 쿼리 매개 변수로 매개 변수가 전송되고 (GET 요청과 함께 본문이 전송되지 않음) Express는 쿼리 매개 변수를 req.query에 넣습니다.

+0

고마워요. –

0

나는 당신과 같이 호출해야한다고 생각 :

fetch('......') 
     .then((response) => response.json()) 
     .then((responseJson) => { 
     return responseJson.movies; 
}) 
.catch((error) => { 
     console.error(error); 
}); 
관련 문제