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.
See Question&Answers more detail:
os