我只改变路线方法,而不是改变路线方法,而不是#34; Post" to" Put",由于某种原因打破它。
POST路由正常:
在带有POST的App.js路线中:
app.post('/editlisting/update',listing.update);
提交给它的editListing.jade文件:
extends layout
block content
div(class="container")
ul(class="userlist")
each listing, i in listings
li
h1 #{listing.name}
form(method='post', action='update' class='regform')
label Name:
input(type='text', name='name', value='#{listing.name}')
br
label Number of Guests:
input(type='text', name='noGuests', value='#{listing.noGuests}')
input(type='hidden', name='id', value='#{listing._id}')
label Price:
input(type='text', name='price', value='#{listing.price}')
input(type='submit', value='Update')
a(href='/') Home
listing.js中的Update方法(我根本不改变它):
exports.update = function(req,res){
var conditions = {_id:req.body.id}
, update = {
name: req.body.name,
noGuests: req.body.noGuests,
price: req.body.price
}
, options = { multi: false };
Listing.update(conditions, update, options, callback);
function callback (err, numAffected) {
if(err) { throw err; }
res.redirect('/');
};
}
然后当我尝试将其更改为PUT ....
app.js:
app.put('/editlisting/update',listing.update);
editListing.Jade:
扩展布局
block content
div(class="container")
ul(class="userlist")
each listing, i in listings
li
h1 #{listing.name}
form(method='put', action='update' class='regform')
label Name:
input(type='text', name='name', value='#{listing.name}')
br
label Number of Guests:
input(type='text', name='noGuests', value='#{listing.noGuests}')
input(type='hidden', name='id', value='#{listing._id}')
label Price:
input(type='text', name='price', value='#{listing.price}')
input(type='submit', value='Update')
a(href='/') Home
当我按下"更新"这会导致我的浏览器显示此错误按钮:
4 | div(class =" container")5 | ul(class =" userlist")> 6 |每个列表,我在列表7 | li 8 | h1#{listing.name} 9 |表格(方法=' put',action =' update' class =' regform')无法读取属性'长度'未定义的
它指向我的editListing.jade文件中的行:
each listing, i in listings
有谁知道问题是什么?
答案 0 :(得分:2)
您可以做的最简单的方法是方法覆盖
运行npm install method-override
在身体解析器之后将它包含在您的应用程序中 像这样的东西
var express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override')
app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
然后修改你的HTML
block content
div(class="container")
ul(class="userlist")
each listing, i in listings
li
h1 #{listing.name}
form(method='POST', action='/editlisting/update?_method=PUT' class='regform')
label Name:
input(type='text', name='name', value='#{listing.name}')
br
label Number of Guests:
input(type='text', name='noGuests', value='#{listing.noGuests}')
input(type='hidden', name='id', value='#{listing._id}')
label Price:
input(type='text', name='price', value='#{listing.price}')
input(type='submit', value='Update')
a(href='/') Home
然后你就可以使用
了app.put('/editlisting/update',listing.update);