Hello when i try to update user on page i get error Cannot POST /update-user cannot find there is a problem can someone see there is a mistake was done code below
index.js
$('#add_user').submit(function(event){
alert("Informacija išsaugota!")
})
$('#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("Naudotojas Atnaujintas!")
})
})
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("Ar tikrai norite ištrinti įraša?")){
$.ajax (request).done(function(response){
alert("Naudotojas ištrintas!")
location.reload()
})
}
})
}
ROUTER.JS
const express = require('express');
const route = express.Router()
const services = require('../services/render')
const controller = require('../controller/controller')
/**
* @description Root Route
* @method GET/
*/
route.get('/',services.homeRoutes)
/**
* @description add users
* @method GET/ add-user
*/
route.get('/add-user',services.add_user)
/**
* @description for update user
* @method GET/ update-user
*/
route.get('/update-user',services.update_user)
// API
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>
<%}%>
Try to search andchange some methods but nothing works and stuck now its can be wrong ejs i gues . Or something else . And how to consolelog all details coming from backend or from front end ?
CodePudding user response:
route.get('/update-user',services.update_user)
Is defined as GET, but you're posting to it - Change to
route.post('/update-user',services.update_user)
$.ajax (request).done(function(response){
console.log(response) <------------------------------------
alert("Naudotojas Atnaujintas!")
})
CodePudding user response:
change get
to post
in this line
route.get('/update-user',services.update_user)
to
route.post('/update-user',services.update_user)
because your submit is post
method
and if need add app.use(express.json())
and app.use(express.urlencoded({extended:false}));
to express