无法使用 Node.js Express MongoDB Mongoose CoffeeScript 进行 POST

发布于 2024-12-09 14:55:51 字数 1390 浏览 1 评论 0原文

更新:我找到了解决方案,请查看本页底部...

我有一个用 CoffeScript 编写的 Node.js、Express、MongoDB、Mongoose 项目,我可以创建和读取数据,但我无法更新。

这就是我的代码的样子;

app.js

 # update
 app.put "/admin/:id.:format?", (req, res) ->
    Content.findById req.body.content.id, (err, c) ->
        c.title = req.body.content.title
        c.body = req.body.content.body
        c.save (err) ->
            switch req.params.format
                when "json"
                    res.send c.__doc
                else
                    res.redirect "/admin"

edit.jade

h2 Edit Content
form(method='post', action='/admin/' + c.id)
  input(name='content[id]', value=c.id, type='hidden')
  input(name='_method', value='PUT', type='hidden')
div
  label Title:
    input(name='content[title]', value=c.title || '')
div
  label Body:
    textarea(name='content[body]')=c.body || ''
div
  input(type='submit', value='Save')

这就是我的控制台所说的

127.0.0.1 - - [Thu, 13 Oct 2011 21:39:55 GMT] "POST /admin/4e96ec17fd7da7cb18000001 HTTP/1.1" 404 - "http://localhost:1234/admin/4e96ec17fd7da7cb18000001/edit" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.83 Safari/535.2"

这就是我的浏览器告诉我的

Cannot POST /admin/4e96ec17fd7da7cb18000001

Update: I found the solution, look in the bottom of this page...

I've got a Node.js, Express, MongoDB, Mongoose project written in CoffeScript and I can create and read data, but I can't update.

This is what my code looks like;

app.js

 # update
 app.put "/admin/:id.:format?", (req, res) ->
    Content.findById req.body.content.id, (err, c) ->
        c.title = req.body.content.title
        c.body = req.body.content.body
        c.save (err) ->
            switch req.params.format
                when "json"
                    res.send c.__doc
                else
                    res.redirect "/admin"

edit.jade

h2 Edit Content
form(method='post', action='/admin/' + c.id)
  input(name='content[id]', value=c.id, type='hidden')
  input(name='_method', value='PUT', type='hidden')
div
  label Title:
    input(name='content[title]', value=c.title || '')
div
  label Body:
    textarea(name='content[body]')=c.body || ''
div
  input(type='submit', value='Save')

And this is what my console says

127.0.0.1 - - [Thu, 13 Oct 2011 21:39:55 GMT] "POST /admin/4e96ec17fd7da7cb18000001 HTTP/1.1" 404 - "http://localhost:1234/admin/4e96ec17fd7da7cb18000001/edit" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.83 Safari/535.2"

And this is what my browser tells me

Cannot POST /admin/4e96ec17fd7da7cb18000001

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

摇划花蜜的午后 2024-12-16 14:55:51

该表单向服务器发送 POST 请求,但您的路由用于 PUT 请求。

The form sends a POST request to the server, but your route is for PUT requests.

面犯桃花 2024-12-16 14:55:51

我在 http://expressjs.com/guide.html 上找到了解决方案:

“当使用诸如使用表单进行 PUT,我们可以利用名为 _method 的隐藏输入,该输入可用于更改 HTTP 方法。为此,我们首先需要 methodOverride 中间件,应将其放置在 bodyParser 下方,以便它。可以利用它的 req.body 包含形式值。”

app.use express.bodyParser()
app.use express.methodOverride()

I found the solution on http://expressjs.com/guide.html:

"When using methods such as PUT with a form, we can utilize a hidden input named _method, which can be used to alter the HTTP method. To do so we first need the methodOverride middleware, which should be placed below bodyParser so that it can utilize it’s req.body containing the form values."

app.use express.bodyParser()
app.use express.methodOverride()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文