使用 Mongoose、Express、NodeJS 更新模型

发布于 2024-10-18 04:40:16 字数 1334 浏览 2 评论 0原文

我正在尝试更新 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 技术交流群。

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

发布评论

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

评论(4

等数载,海棠开 2024-10-25 04:40:16

您必须在更新任何内容之前找到该文档:

Place.findById(req.params.id, function(err, p) {
  if (!p)
    return next(new Error('Could not load Document'));
  else {
    // do your updates here
    p.modified = new Date();

    p.save(function(err) {
      if (err)
        console.log('error')
      else
        console.log('success')
    });
  }
});

使用与您相同的设置在生产代码中为我工作。您可以使用 mongoose 提供的任何其他查找方法来代替 findById。只需确保在更新之前获取该文档即可。

You have to find the document before updating anything:

Place.findById(req.params.id, function(err, p) {
  if (!p)
    return next(new Error('Could not load Document'));
  else {
    // do your updates here
    p.modified = new Date();

    p.save(function(err) {
      if (err)
        console.log('error')
      else
        console.log('success')
    });
  }
});

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.

天邊彩虹 2024-10-25 04:40:16

现在,我认为你可以这样做:

Place.findOneAndUpdate({name:req.params.name}, req.body, function (err, place) {
  res.send(place);
});

你也可以通过 id 找到:

Place.findOneAndUpdate({_id:req.params.id}, req.body, function (err, place) {
  res.send(place);
});

Now, i think you can do this :

Place.findOneAndUpdate({name:req.params.name}, req.body, function (err, place) {
  res.send(place);
});

You can find by id too :

Place.findOneAndUpdate({_id:req.params.id}, req.body, function (err, place) {
  res.send(place);
});
执妄 2024-10-25 04:40:16

所以现在你可以直接通过 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);
});

So now you can find and update directly by id, this is for Mongoose v4

Place.findByIdAndUpdate(req.params.id, req.body, function (err, place) {
  res.send(place);
});

Just to mention, if you needs updated object then you need to pass {new: true} like

Place.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, place) {
  res.send(place);
});
夏末的微笑 2024-10-25 04:40:16

我认为你的问题是你正在使用节点 0.4.0 - 尝试移动到 0.2.6 它应该可以工作。 github 上记录了一个问题,bodyDecoder 未填充节点 >= 0.3.0 中的 req.body.variable 字段。

I think your problem is that you are using node 0.4.0 - try moving to 0.2.6 with an it should work. There is an issue logged on github with the bodyDecoder not populating the req.body.variable field in node >= 0.3.0.

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