I'm having trouble with my update function. It's not a database issue because delete works fine.
Here is the code for my Update route:
Can someone please assist me? I've been using console.log to display values in the console, and they all seem correct.
I have already tried logging values during the "update" process.
C:\Personal\Books\Trials>node app.js
Trials is running on port 3000
Success Update
5cc86fa0b5e2143684378877
Jim4
{ _id: 5cc86fa0b5e2143684378877,
fieldname: 'Jim3',
fieldtype: 'string',
fieldinputtype: 'textbox',
__v: 0 }
app.put("/adminstudy/:id",function(req,res){
studyfields.findByIdAndUpdate(req.params.id, req.body.fieldname, function(err, returnedstudyfield){
if(err){
console.log(err);
res.redirect("/adminstudy");
} else {
console.log("Success Update");
console.log(req.params.id);
console.log(req.body.fieldname);
console.log(returnedstudyfield);
res.redirect("/adminstudy");
}
});
});
Below is the form that submits data to the update route:
<form action="/adminstudy/<%= editedstudyfield._id %>?_method=PUT" method="POST">
<div class="form-group">
<input class="form-control" type="text" name="fieldname" value="<%= editedstudyfield.fieldname %>">
</div>
<div class="form-group">
<select name="fieldtype">
<option value="string">Text</option>
<option value="number">Number</option>
</select>
</div>
<div class="form-group">
<select name="fieldinputtype">
<option value="textbox">Text Box</option>
<option value="Textarea">Text Area</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-lg btn-block btn-success" type="submit">UPDATE THIS STUDY FIELD</button>
</div>
</form>
I expected the update to work, but it doesn't. Any assistance would be greatly appreciated.