Mongoose - 使用嵌入式文档保存模型的表单 [英] Mongoose - Form to save model with embedded documents

查看:15
本文介绍了Mongoose - 使用嵌入式文档保存模型的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将嵌入式数组保存到 Mongoose 模型.

Having trouble saving an embedded array to a Mongoose model.

请参阅底部的编辑.

我有一个表单可以使用 Mongoose 在 Express 中创建 BlogPost 以将数据存储在 mongo 中.我可以创建和查看新的博客文章,但是我只是在 BlogPost 模型中添加了一个嵌入式文档架构 Feed,我无法将 Feed 数组从表单保存到模型中

I have a form to create BlogPost in Express using Mongoose to store data in mongo. I can create and view new blogposts however I just added an embedded document schema Feed into the BlogPost model and I can't get Feed arrays to save from the form into the model

代码:

博客帖子.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');

var Schema = mongoose.Schema
  , ObjectId = Schema.ObjectId;

var Feeds = new Schema({
    name      : { type: String }
  , key       : { type: String }
});

var BlogPost = new Schema({
    author    : ObjectId
  , title     : { type: String, required: true, index: { unique: true } }
  , date      : { type: Date, required: true, default: Date.now }
  , feeds     : [Feeds]
});

mongoose.model('BlogPost', BlogPost);

web.js

...
app.get('/blogpost/new', function(req, res) {
    res.render('blogposts/blogpost_new.jade', { locals: {
        title: 'New BlogPost'
    }
    });
});

app.post('/blogpost/new', function(req, res){
    var b = new BlogPost(req.body.b)
    b.save(function() {
      b.feeds.push();
      res.redirect('/blogposts');
    });
});
...
var BlogPost = mongoose.model('BlogPost', BlogPost);

玉形态

form( method="post")
  div
    div
      span Title :
      input(type="text", name="b[title]", id="editBlogPostTitle")
    div 
      span Feeds :
      ul 
        li 
          span name
          textarea( name="f[name]", rows=20, id="editBlogPostBodyName")
        li
          span key
          textarea( name="f[key]", rows=20, id="editBlogPostBodyKey")
    div#editBlogPostSubmit
      input(type="submit", value="Send")

如果我填写此表单,模型会发布并保存,但提要数据不存在(提要":[ ]).

If I fill out this form, the model posts and saves but the feeds data isn't there ("feeds" : [ ]).

我应该如何正确提交供稿数据以保存到数组?

How should I properly submit the feeds data to save to the array?

编辑

所以我设法设置了一个表单来保存一个带有 namekey 的 Feed 对象,并在博客帖子中执行以下操作.但是,这仍然需要改进以允许在创建单个 BlogPost 时保存多个 Fe​​ed.使用我当前的解决方案,我只能正确保存一个 Feed.想法?

So I have managed to set up a form to save a Feed object with name and key within a BlogPost doing the following. However, this still needs to be improved to allow for multiple Feeds to be saved at the time of creating a single BlogPost. With my current solution I can only save one Feed properly. Thoughts?

blogposts.js(只需将 Feed 更改为 Feed

blogposts.js (just change Feeds to Feed

var Feed = new Schema({
...

web.js(只是移动了推送)

web.js (just moved the push)

app.post('/blogpost/new', function(req, res){
    var b = new BlogPost(req.body.b)
    b.feeds.push(req.body.feed);
    b.save(function() {
      res.redirect('/blogposts');
});

});

表单(只需更改提要名称)

form (just change feed names)

    li
        span name
        textarea( name="feed[name]", rows=20, id="editBlogPostBodyKey")
    li
        span key
        textarea( name="feed[key]", rows=20, id="editBlogPostBodyKey")

这可以正确保存,我只是无法在保存时在一篇博文中创建多个提要.非常感谢任何帮助.谢谢.

This saves properly, I just can't create multiple feeds within a blogpost at the time of saving. Any help greatly appreciated. thanks.

推荐答案

就其价值而言,我确实让它起作用了.查看我上面的编辑,一旦我在提要名称中添加了 [],它就起作用了:"feed[0][name]" 和 "feed[0][key]",然后是 "feed[1][name]"和feed[1][key]".感谢机会的一些好主意.对我来说,将该逻辑放在获取"路线中是行不通的,它需要放在帖子"中.非常感谢.

For what it's worth, I did get this to work. See my edit above which worked once I added an [] to the feed name: "feed[0][name]" and "feed[0][key]", and then "feed[1][name]" and "feed[1][key]". Thanks Chance for some good ideas. For me it didn't work to put that logic in the 'get' route, it needed to be in the 'post'. Thanks a lot tho.

这篇关于Mongoose - 使用嵌入式文档保存模型的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆