Trying to do a put request to update data. I am having trouble getting access to the data in the request object or should I say getting the form field values into the request object.
here is the function thats trying to perform the fetch:
function EditComment(e){
const input= document.getElementById('editInputs')
e.preventDefault();
console.log('inside EditCOmment the id is :'+newId)
fetch('/update/' + newId, {
method: 'Put',
headers : {
"Content-Type" : "application/json; charset=utf-8"
},
body : JSON.stringify({todo : input.value()})
})
.then(response => response.json())
.then(function (response) {
if (response.status === 'success') {
console.log(response)
}
})
.catch(err => {
console.log('fetch update didn\'t succeed\n' + err);
});
}
here is the form its trying to get the data from:
<form id="editcommentForm" method="put" /*action="/update"*/ >
<div class="form-group">
<label>Name</label><input type="text" class='form-control' name="client-name" placeholder="Enter name" required>
</div>
<div class="form-group">
<label>Comments</label><textarea id="editInputs" class="form-control" name="comment" placeholder="Enter Comments "rows="3"></textarea>
</div>
<button id="edit-btn" type="submit" class="btn btn-primary btn-lg btn-block">Submit Data</button>
</form>
and here is my route:
const updateComment = (req, res) => {
const userInput= req.body;
console.log(userInput);
console.log(req.params);
console.log('***********************************')
findOneAndUpdate({_id: req.params.id},{$set: {name: userInput['client-name'], comment: userInput.comment}},{new: true}, function(err, data){
if (err) {
res.send({
status: 'failure',
error: err
});
return;
}
res.json({
comments,
status: 'success'
});
})
};
I am getting findOneAndUpdate is not defined. And the req.body is completely empty, what am I doing wrong?
I am trying to find a record by Id, and update its values.
Edit: and Im gettting a typeError in the browser console: input.value is not a function