我试图打一条特定的路线:
http://127.0.0.1:3333/store/products?productId=4
但是服务器给了我这个错误:
"message": "E_ROUTE_NOT_FOUND: Cannot DELETE:/store/products",
"stack": "HttpException: E_ROUTE_NOT_FOUND: Cannot PATCH:/store/products\n
我试图打一条特定的路线:
http://127.0.0.1:3333/store/products?productId=4
但是服务器给了我这个错误:
"message": "E_ROUTE_NOT_FOUND: Cannot DELETE:/store/products",
"stack": "HttpException: E_ROUTE_NOT_FOUND: Cannot PATCH:/store/products\n
除了@crbast 提出的要点:您的代码似乎使用了 HTTP PATCH 方法(https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH)而不是 HTTP DELETE 您预计
你没有点击正确的网址,你route
错了。
您的正确网址route.js
是:
http://127.0.0.1:3333/store/products/4
^- Product id
和route
:
Route.delete('/products/:productId', 'ProductsController.delete')
// ^- use : for url parameter
请求正文 (json)。
文档:https ://preview.adonisjs.com/guides/http/form-submissions#reading-form-data
示例网址:
http://127.0.0.1:3333/products?name=hello
路线示例:
Route.post('/products', 'MyController.myFunction')
控制器 :
public async myFunction ({ request }: HttpContextContract) {
const data = request.only(['name'])
// ...
}
指定动态 url 参数。
文档:https ://preview.adonisjs.com/guides/http/routing#dynamic-urls
示例网址:
http://127.0.0.1:3333/products/1
路线示例:
Route.post('/products/:id', 'MyController.myFunction')
控制器 :
public async myFunction ({ params }: HttpContextContract) {
const id = params.id
// ...
}