无法使用Node.js Express MongoDB Mongoose CoffeeScript

编程入门 行业动态 更新时间:2024-10-23 23:22:20
本文介绍了无法使用Node.js Express MongoDB Mongoose CoffeeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

更新:我找到解决方案,查看此页面的底部...

我有一个Node.js ,Express,MongoDB,Mongoose项目写在CoffeScript和我可以创建和读取数据,但我不能更新。

这是我的代码看起来像; p>

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) - > 切换req.params.format 当json res.send c .__ doc else res.redirect/ admin

edit.jade

h2编辑内容 form(method ='post',action ='/ admin /'+ c.id) input(name ='content [id]',value = c.id,type = 'hidden') input(name ='_ method',value ='PUT',type ='hidden') div 标签: input [title]',value = c.title ||'') div 标签Body: textarea(name ='content [body]')= c.body || '' div input(type ='submit',value ='Save')

$ b b

这是我的控制台说

127.0.0.1 - - [Thu,2011年10月13日21:39: 55 GMT]POST / admin / 4e96ec17fd7da7cb18000001 HTTP / 1.1404 - http:// localhost:1234 / admin / 4e96ec17fd7da7cb18000001 / editMozilla / 5.0(Macintosh; Intel Mac OS X 10_7_1)AppleWebKit / 535.2 Gecko)Chrome / 15.0.874.83 Safari / 535.2

这是我的浏览器告诉我

无法POST / admin / 4e96ec17fd7da7cb18000001

解决方案

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

当使用PUT等表单的方法时,我们可以使用一个名为_method的隐藏输入,为了这样做,我们首先需要methodOverride中间件,它应该放在bodyParser 下面,以便它可以利用它的req.body包含表单值。

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

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 - "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

解决方案

I found the solution on expressjs/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()

更多推荐

无法使用Node.js Express MongoDB Mongoose CoffeeScript

本文发布于:2023-11-22 19:17:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Express   js   Node   CoffeeScript   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!