I am facing problem with understanding how input from tag can be converted in JSON and then posting it to my api.
My Frontend code is
<div>
<form className="AddMentor">
<table>
<tr>
<td> <label>MentorID<input type="text" name="MentorID" /></label> </td>
<td> <label>MentorName<input type="text" name="MentorNAME" /></label> </td>
<td> <label>MentorEMAIL<input type="text" name="MentorEMAIL" /></label> </td>
<td> <label>MentorPH<input type="text" name="MentorPH" /></label> </td>
<td> <label>MentorLINKEDIN<input type="text" name="MentorLINKEDIN" /></label></td>
<td> <label>MentorPM<input type="text" name="MentorPM" /> </label> </td>
</tr>
</table>
<button type="submit" id="save">Add Mentor</button>
</form>
</div>
And MY api code is
//ADD MENTOR API
router.route('/MentorEle').post((request,response)=>{
let Mentor = {...request.body}
dboperations.addMentor(Mentor).then(result => {
response.json(result);
})
})
I tried this for converting it into JSON and POSTING to my api
$(document).on('click', '#save', function(event) {
event.preventDefault();
var data = $('tr').map(function() {
var obj = {};
$(this).find('input, select').each(function() {
obj[this.name] = $(this).val();
});
return obj;
}).get();
console.log(JSON.stringify(data));
});
It didn't work unfortunately, I faced several errors and the current error is
Module not found: Error: Cannot find file: 'node-jquery.js' does not match the corresponding name on disk: '.\node_modules\jquery\lib\jQuery'.
please anyone help me with this code or ajax approach of it