使用 Mongoose、Express、NodeJS 更新模型
我正在尝试更新 MongoDB 中的实例化模型(“Place”-我知道它可以从其他路径工作),并且花了一段时间尝试正确执行此操作。我还尝试重定向回查看“地点”的页面以查看更新的属性。
Node v0.4.0、Express v1.0.7、Mongoose 1.10.0
架构:
var PlaceSchema = new Schema({
name :String
, capital: String
, continent: String
});
控制器/路由:
app.put('/places/:name', function(req, res) {
var name = req.body.name;
var capital = req.body.capital;
var continent = req.body.continent;
Place.update({ name: name, capital: capital, continent: continent}, function(name) {
res.redirect('/places/'+name)
});
});
我尝试了很多不同的方法,但似乎无法得到它。
另外,我声明这三个{name、capital和Continental}变量不是会阻止进一步的操作吗?谢谢。一般调试帮助也值得赞赏。 Console.log(name)(位于声明下方)不记录任何内容。
玉石形态:
h1 Editing #{place.name}
form(action='/places/'+place.name, method='POST')
input(type='hidden', name='_method', value='PUT')
p
label(for='place_name') Name:
p
input(type='text', id='place_name', name='place[name]', value=place.name)
p
label(for='place_capital') Capital:
p
input(type='text', id='place_capital', name='place[capital]', value=place.capital)
p
label(for='place_continent') Continent:
p
textarea(type='text', id='place_continent', name='place[continent]')=place.continent
p
input(type="submit")
I'm trying to update an instantiated model ('Place' - I know it works from other routes) in a MongoDB and have spent a while trying to properly do so. I'm also trying to redirect back to the page that views the 'place' to view the updated properties.
Node v0.4.0, Express v1.0.7, Mongoose 1.10.0
Schema:
var PlaceSchema = new Schema({
name :String
, capital: String
, continent: String
});
Controller/route:
app.put('/places/:name', function(req, res) {
var name = req.body.name;
var capital = req.body.capital;
var continent = req.body.continent;
Place.update({ name: name, capital: capital, continent: continent}, function(name) {
res.redirect('/places/'+name)
});
});
I've tried a bunch of different ways but can't seem to get it.
Also, isn't how I declare the three {name, capital, and continent} variables blocking further operations? Thanks. General debugging help is also appreciated. Console.log(name) (right below the declaration) doesn't log anything.
Jade form:
h1 Editing #{place.name}
form(action='/places/'+place.name, method='POST')
input(type='hidden', name='_method', value='PUT')
p
label(for='place_name') Name:
p
input(type='text', id='place_name', name='place[name]', value=place.name)
p
label(for='place_capital') Capital:
p
input(type='text', id='place_capital', name='place[capital]', value=place.capital)
p
label(for='place_continent') Continent:
p
textarea(type='text', id='place_continent', name='place[continent]')=place.continent
p
input(type="submit")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
所以现在你可以直接通过 id 查找和更新,这是针对 Mongoose v4
Place.findByIdAndUpdate(req.params.id, req.body, function (err, place) {
res.send(place);
});
只是提一下,如果你需要更新对象那么你需要传递 {new: true}
就像
Place.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, place) {
res.send(place);
});
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您必须在更新任何内容之前找到该文档:
使用与您相同的设置在生产代码中为我工作。您可以使用 mongoose 提供的任何其他查找方法来代替 findById。只需确保在更新之前获取该文档即可。
You have to find the document before updating anything:
works for me in production code using the same setup you have. Instead of findById you can use any other find method provided by mongoose. Just make sure you fetch the document before updating it.