Greetings! I am encountering an error when attempting to update a user on a page. The error message "Cannot POST /update-user" keeps popping up, indicating that there might be a mistake in the code. Can someone please take a look and help identify the issue? You can find the code below for reference. https://i.sstatic.net/54DDz.png
index.js
$('#add_user').submit(function(event){
alert("Information saved!")
})
$('#update-user').submit(function(event){
event.preventDefault();
let unindexed_array =$(this).serializeArray();
let data = {}
$.map(unindexed_array,function(n,i){
data[n['name']]= n['value']
})
console.log(data)
let request = {
"url" : `http://localhost:3000/api/users/${data.id}`,
"method" : "PUT",
"data" : data
}
$.ajax (request).done(function(response){
alert("User Updated!")
})
})
if(window.location.pathname == "/"){
$ondelete=$(".table tbody td a.delete");
$ondelete.click(function(){
let id = $(this).attr("data-id")
let request = {
"url" : `http://localhost:3000/api/users/${id}`,
"method" : "DELETE",
}
if(confirm("Are you sure you want to delete this record?")){
$.ajax (request).done(function(response){
alert("User deleted!")
location.reload()
})
}
})
}
ROUTER.JS
const express = require('express');
const route = express.Router()
const services = require('../services/render')
const controller = require('../controller/controller')
route.get('/',services.homeRoutes)
route.get('/add-user',services.add_user)
route.get('/update-user',services.update_user)
route.post('/api/users',controller.create);
route.get('/api/users',controller.find);
route.put('/api/users/:id',controller.update);
route.delete('/api/users/:id',controller.delete);
module.exports = route
_show.ejs
<%for(var i = 0; i <users.length; i++){%>
<tr>
<td><%= i + 1%></td>
<td><%=users[i].name%></td>
<td><%=users[i].email%></td>
<td><%=users[i].Lytis%></td>
<td><%=users[i].status%></td>
<td>
<a href="/update-user?id=<%=users[i]._id%>" class="btn border-shadow update">
<span class="text-gradient"><i class="fas fa-user-edit"></i></span>
</a>
<a class="btn border-shadow delete" data-id=<%=users[i]._id%>>
<span class="text-gradient"><i class="fas fa-user-slash"></i></span>
</a>
</td>
</tr>
<%}%>
Struggling to troubleshoot and resolve the issue, despite trying various methods. It could be related to the ejs file or something else entirely. Also, seeking guidance on how to console log all details from the backend or frontend. Any assistance would be highly appreciated.