Reputation: 565
I have a URL
which I want to send
to the server
with ajax
in order to delete
products based on id
. How can I receive
that with NodeJS
just as I would do this in the PHP api
so :
$sProductId = $_GET['id'];
This is the URL from the ajax call:
var sUrl = "/delete-product" + sProductId;
This is my server.js
app.post('/delete-product' ???, upload.none(), (req, res) => {
global.sUpdateProductImagePath = req.file.path.split("public/")[req.file.path.split("public").length - 1]
user.deleteProduct(req.body, (err, jResult) => {
if (err) {
console.log(jResult)
return res.send(jResult)
}
console.log(jResult)
return res.send(jResult)
})
})
And my user.js:
user.deleteProduct = (jProductData, fCallback) => {
global.db.collection('products').deleteOne({ "_id": "" }, (err, jResult) => {
if (err) {
var jError = { "status": "error", "message": "ERROR -> deleteProduct -> user.js -> 001" }
return fCallback(false, jError)
}
var jOk = { "status": "ok", "message": "user.js -> product deleted -> 000" }
console.log(jResult)
return fCallback(false, jOk)
})
}
Upvotes: 1
Views: 48
Reputation: 91
you can do like this if you want id in path params
app.post('/delete-product/:id', upload.none(), (req, res) => {var id= req.params.id; // this id is from path params
you can also send id in search query like /delete-product?id=yourid
app.post('/delete-product', upload.none(), (req, res) => {var id= req.query.id;
Upvotes: 1
Reputation: 1109
The way you are passing the parameters to send it from the client to the server is a get method. The id is part of the url.
So, in the server you should change the post for get and add /:idproduct to receive it.
Also, instead of req.body, you should change to req.params.:
For example:
app.get('/delete-product/:idproduct', (req, res) => {
//global.sUpdateProductImagePath = req.file.path.split("public/")[req.file.path.split("public").length - 1]
//req.body = req.params.idproduct
user.deleteProduct(req.params.idproduct, (err, jResult) => {
if (err) {
console.log(jResult)
return res.send(jResult)
}
console.log(jResult)
return res.send(jResult)
})
})
Upvotes: 0