Mongoose - 使用嵌入式文档保存模型的表单 [英] Mongoose - Form to save model with embedded documents
问题描述
请参阅底部的修改。
我有一个表单在Express中使用Mongoose创建BlogPost以在mongo中存储数据。我可以创建和查看新的blogposts,但是我刚刚将一个嵌入式文档模式Feed添加到BlogPost模型中,我无法将Feed数组从表单保存到模型中。
代码:
BlogPosts.js
var mongoose = require '猫鼬');
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',{localals: {
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);
翡翠表单
code> form(method =post)
div
div
span标题:
input(type =text,name =b [title] ,id =editBlogPostTitle)
div
span Feeds:
ul
li
span name
textarea(name =f [name], row = 20,id =editBlogPostBodyName)
li
span key
textarea(name =f [key],rows = 20,id =editBlogPostBodyKey)
div#editBlogPostSubmit
input(type =submit,value =Send)
如果我填写此表单,模型发布和保存,但Feed数据不存在(feeds:[]
)。
如何正确提交Feed数据以保存到数组?
修改
所以我设法使用名称
和键
在一个BlogPost做的follo翅膀。然而,这仍然需要改进,以便在创建单个BlogPost时保存多个Feed。使用我目前的解决方案,我只能正确保存一个Feed。想法?
blogposts.js(只需将Feed更改为Feed
var Feed = new Schema({
...
web.js(刚刚移动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 ');
});
});
表单(只是更改Feed名称)
li
span name
textarea (name =feed [name],rows = 20,id =editBlogPostBodyKey)
li
span key
textarea(name =feed [key],rows = id =editBlogPostBodyKey)
保存正确,我无法在一个blogpost内创建多个Feed在保存的时候,非常感谢,谢谢。
我确实得到了这个工作。看到我上面的编辑工作,一旦我添加了一个[]到Feed的名称:feed [0] [name]和feed [0] [key]
然后feed [1] [name]和feed [1] [key]
。感谢可能有一些好的想法。对于我来说,把这个逻辑放在'获取'的路线上并不奏效,它需要在'post'中。非常感谢。
Having trouble saving an embedded array to a Mongoose model.
Please see Edit at bottom.
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
code:
BlogPosts.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);
Jade form
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?
Edit
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 (just change Feeds to Feed
var Feed = new Schema({
...
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.
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屋!