I need to get some data from my database with ajax. I'm using Node JS and Express to handle routes. This is the backend code:
router.get('/isAuth', ensureLoggedIn, (req, res) => {
Permit.find({}).populate('authBy').exec()
.then(permits => res.jsonp(permits))
.catch(err => res.send(err));
});
And this is the client side: const allPermits = document.getElementById('allPermits'), table = document.querySelector('table tbody');
allPermits.addEventListener('click', getData);
function getData() {
let xhr = new XMLHttpRequest();
xhr.open('get', '/permits/isAuth', true);
xhr.onload = () => {
if (this.status == 200) {
let permitData = this.responseText;
let responseData = '';
permitData.forEach((permit) => {
responseData +=
`<tr class="dataRow">
<td>${permit.name}</td>
<td>${permit.destination}</td>
<td>${permit.fromD}</td>
<td>${permit.toD}</td>
<td>${permit.notes}</td>
<td>
<button class="deletePermit" data-id="${permit._id}"><i class="fa fa-trash"></i></button>
</td>
</tr>`;
})
table.innerHTML = responseData;
}
}
xhr.send();
}
The issue is that this line doesn't work: table.innerHTML = responseData;
.
I also tried to console.log(responseData)
but other than the success message of the ajax request I don't see anything - https://image.prntscr.com/image/6Otihd6jSMWPRdoz4y6y9g.png;
Thanks!