У меня есть метод ax ios GET, и мне нужно передать параметры с помощью запроса на получение. Я вызываю метод GET на стороне клиента (React. js) следующим образом:
const [Cart] = useState([]);
const user = {
userId : session.userId
};
console.log(user);
useEffect((user) => {
if(session.userId !== null){
//Axios.get('http://localhost:5000/api/cart/getCart', user) <- I tried both these ways
Axios({
method: 'GET',
url: 'http://localhost:5000/api/cart/getCart',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {},
params: {
"userId" : session.userId
}
})
.then(res => {
const cart = res.data;
let tempProducts = [];
cart.data.forEach(item => {
const singleItem = {...item};
tempProducts = [...tempProducts, singleItem];
});
this.setState(() => {
return {Cart: tempProducts};
});
})
}
console.log(Cart)
});
Но на стороне сервера (node.js) он не получает значение параметра.
router.get('/getCart', (req, res) => {
console.log(req.body.userId) //This gives the output as undefined
User.findOne({_id: req.body.userId}
,(err, userInfo) => {
res.json(userInfo.Cart);
})
});
Я реализовал раскомментированный запрос ax ios .get, сославшись на this . Не могли бы вы помочь мне найти ошибку? Или вы можете предложить мне какой-нибудь другой способ сделать это? Спасибо