I’m trying to create a PUT request using Axios, what I want to do is update a user’s information once a button is pressed,
Button code:
<button id="evaluator-button" class="button-submit" onclick="ft_evaluate()">Submit</button>
function ft_evaluate () {
console.log(sessionStorage.getItem('Counter') + ' of ' + sessionStorage.getItem('Max'));
let evaluationPoints = document.getElementById('evaluator-input').value;
let userLogin = document.getElementById('match-login').innerHTML;
let actualPoints = document.getElementById('match-points').innerHTML;
if (evaluationPoints == 0 || !userLogin) {
return ;
}
else {
evaluationPoints = evaluationPoints * 10 / 100;
actualPoints = parseFloat(actualPoints.replace(/[^0-9]/g, '')) / 100;
let newPoints = actualPoints + evaluationPoints;
newPoints = newPoints.toFixed(2);
console.log(newPoints, actualPoints, evaluationPoints, userLogin);
fetch('http://localhost:3005/script/data', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
login: userLogin,
points: newPoints
})
});
}
}
And this is the controller code:
const scriptPutData = (req, res) => {
authUser.find({}, async (err, data) => {
if (err) {
console.error(clc.red(`${err}`));
}
else {
if (data.find(dbUser => dbUser.login === req.session.login)) {
// What i should do inside this block to update the info?
}
else {
res.redirect('/login');
return;
}
}
});
}
The info of the user have this format:
[
{
"login": "ezidane-",
"name": "Enzo Zidane",
"points": 0.45,
"nacionality": "French",
"bool_malaga": "1",
"bool_andalucia": "1",
"city": "Vélez-Málaga",
"laboral_status": "Desempleado/a",
"image": "https://xxxxxxxxxx.jpg",
"pmonth": "february",
"pyear": "2022",
"motivational": "https://api.xxxxxx.jpg"
}
]
I just wanna update the points for each user [login] who press the button.
Thanks in advance.