I have a problem when try to delete my data in adonisjs, when i enter primarykey as parameter to delete it success, but when i'm try to enter another parameter it doesn't work.
This is my route to access endpoint:
Route.delete('customer_wishlist/:id_product', 'WishlistController.delete')
id_product
isn't primarykey, but foreignkey. This is in my controller to process delete:
async delete ({params, response}) {
console.log(params)
const wishlist = await Wishlist.find(params.id_product)
console.log(wishlist)
if (!wishlist) {
return response.status(404).json({data: 'Resource not found'})
}
await wishlist.delete()
return response.status(200).json({status:true, data: wishlist})
}
The result of console.log above const wishlist = await Wishlist.find(params.id_product)
is json id, this match with parameter. But the result of console log below const wishlist = await Wishlist.find(params.id_product)
is null.
What's wrong with this code?
.find()
is only for primaryKey
try it :
.findBy()
-> documentation
await Wishlist.findBy('id_product', params.id_product)
! Returns the first matching record