Javascript 节点/方法覆盖在放置时给出错误
我在使用方法重写时遇到问题,并试图使用它将其放入Mongo中的项目。因此,我可以通过邮递员拨打电话,并按预期更新文档,但在尝试通过收到的UI进行更新时:Javascript 节点/方法覆盖在放置时给出错误,javascript,node.js,express,Javascript,Node.js,Express,我在使用方法重写时遇到问题,并试图使用它将其放入Mongo中的项目。因此,我可以通过邮递员拨打电话,并按预期更新文档,但在尝试通过收到的UI进行更新时: Request URL:http://localhost:3000/characters/?_method=PUT Request Method:POST Status Code:404 Not Found 以下是路线和路线: app.js const express = require('express'); const bodyParse
Request URL:http://localhost:3000/characters/?_method=PUT
Request Method:POST
Status Code:404 Not Found
以下是路线和路线:
app.js
const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
//
//
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride('_method'));
路线
router.put('/:id', (req, res) => {
Character.findOne({
_id: req.params.id
})
.then(character => {
//
//
character
.save()
.then(character => {
res.redirect('/characters/show/${character.id}');
})
});
还有HTML
<form action="/characters/{{character.id}}?_method=PUT" method="POST" class="col s12">
<input type="hidden" name="_method" value="PUT" />
<input type="submit" value="Save" class="btn">
</form>
谢谢大家,任何帮助都将不胜感激 您没有在路由中为
:id
传递值,并且您没有仅用于/characters/
的路由
URL:http://localhost:3000/characters/?_method=PUT
您需要确保设置了character.id
——在您的示例中它似乎是空的
action=“/characters/{{{character.id}}?\u method=PUT”
您还指定了POST
作为表单的方法,您应该使用PUT
method=“PUT”
如果要使:id
参数成为可选参数,请在参数后面加一个问号:
router.put('/:id?',(req,res)=>{/code>
您还可以通过硬编码一个值来测试路由,看看是否仍然得到404
action=“/characters/123?\u method=PUT”
您没有在路由中传递:id
的值,也没有仅用于/characters/
的路由
URL:http://localhost:3000/characters/?_method=PUT
您需要确保设置了character.id
——在您的示例中它似乎是空的
action=“/characters/{{{character.id}}?\u method=PUT”
您还指定了POST
作为表单的方法,您应该使用PUT
method=“PUT”
如果要使:id
参数成为可选参数,请在参数后面加一个问号:
router.put('/:id?,(req,res)=>{
您还可以通过硬编码一个值来测试路由,看看是否仍然得到404
action=“/characters/123?\u method=PUT”
这是router.put('/characters/:id
还是此路由器已连接到前缀为/characters
的路由器?无论哪种方式,请求的路由都不存在于代码中-缺少:id
。这是router.put吗('/characters/:id
或此路由器是否已连接到前缀为/characters
的路由器?无论哪种方式,请求的路由都不存在于代码中-缺少:id
。OP正在使用方法覆盖中间件,因此方法=“POST”表单上的
应该可以正常工作。OP正在使用方法覆盖
中间件,因此表单上的method=“POST”
应该可以正常工作。