混音运行 - 提交动作并获取“ root” - 没有行动,但您正在尝试提交
我在混音运行中派遣我的诉讼遇到了一些麻烦 - 我还有一个带有购物车中所有数据的旁边 - 我有一个整理所有数据的表格 - 当我希望创建结帐时我想从此处请求的数据中调用操作,
<Form action='/products' method="post">
{cart.map((item, idx) => (
<div key={idx}>
<input readOnly value={item.product.id} type="hidden" name="id"/>
<input readOnly value={item.quantity} type="hidden" name="quantity"/>
</div>
))}
<button
className="mr-2 m"
> Add to Cart
</button>
</Form>
export const action: ActionFunction = async ({request}) => {
// get the form data from the POST
const formData = await request.formData()
const id = formData.getAll('id')
const quantity = formData.getAll('quantity')
const newObj = id.map((data, index) => {
return { variantId: data, quantity: quantity[index] }
} )
const cart = await createCheckout(newObj)
return cart
}
我的结帐URL是生成的,因此我需要等待响应。当我提交时,我会得到一个405错误,说不允许的方法
react_devtools_backend.js:4026 Route "root" does not have an action, but you are trying to submit to it. To fix this, please add an `action` function to the route
是错误消息,但是我似乎找不到文档中的任何地方如何在路由中添加操作函数?因为我发誓我已经在这样做了吗?
Im having a bit of trouble getting my action to dispatch in remix run - I have an Aside which comes out with all the data from my shopping cart - I have a form that collates all the data - and when I want the checkout to be created I want to call the action
<Form action='/products' method="post">
{cart.map((item, idx) => (
<div key={idx}>
<input readOnly value={item.product.id} type="hidden" name="id"/>
<input readOnly value={item.quantity} type="hidden" name="quantity"/>
</div>
))}
<button
className="mr-2 m"
> Add to Cart
</button>
</Form>
export const action: ActionFunction = async ({request}) => {
// get the form data from the POST
const formData = await request.formData()
const id = formData.getAll('id')
const quantity = formData.getAll('quantity')
const newObj = id.map((data, index) => {
return { variantId: data, quantity: quantity[index] }
} )
const cart = await createCheckout(newObj)
return cart
}
From the data that is requested here my checkout URL is generated so i need to wait for the response. When I submit i get a 405 error saying method not allowed
react_devtools_backend.js:4026 Route "root" does not have an action, but you are trying to submit to it. To fix this, please add an `action` function to the route
This is the error message but I cant seem to find anywhere in the docs how to add a action function to the route? because I swear I am already doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tldr;
我遇到了同样的问题,并能够通过更改我的动作URL来解决“最后”索引。
详细信息
我的“产品”文件位于/products/index.tsx上,
以进行混音,以免我指的是root我必须使用以下操作路线:
action =“/products?index”
仅使用
action =“/products”
单独使用。一旦我添加
索引
在操作中,一切都按预期工作。根据混音文档:
有关更多详细信息,请参见: https://remix.run/docs/docs/en/en/v1 /api/remix#form
另外,请注意,大多数情况下,您可以删除操作,并且表单将使用渲染的路线。
tldr;
I ran into this same issue and was able to solve by changing my action url to include ?index at the end.
Details
My "products" file was located at /products/index.tsx
In order for remix to not think I was referring to root I had to use the following action route:
action="/products?index"
Just using
action="/products"
alone did not work.Once I added the
?index
part to the action, everything worked as expected.According to the remix docs:
For more details, see: https://remix.run/docs/en/v1/api/remix#form
Also, note that most of the time you can just leave off the action and the Form will use the route in which it is rendered.