Im struggling to insert array of strings into mongo db collection.
I am generating dynamic form fields for CCs and Attendees, and need to save those values in an array.
its coming up with an error insert failed: Error: 0 must be a string
console.log(attendees
) prints [Object]
here the jQuery snippet:
$(function(){
$(document).on('focus', 'div.form-group-options div.input-group-option:last-child input', function(){
var sInputGroupHtml = $(this).parent().html();
var sInputGroupClasses = $(this).parent().attr('class');
$(this).parent().parent().append('<div class="'+sInputGroupClasses+'">'+sInputGroupHtml+'</div>');
});
$(document).on('click', 'div.form-group-options .input-group-addon-remove', function(){
$(this).parent().remove();
});
});
html:
<form class="row form-horizontal newMeeting">
<div class="form-group form-group-options col-xs-11 col-md-3">
<div class="input-group input-group-option col-xs-12">
<input type="text" name="option[]" class="form-control" id="cc" placeholder="CCs">
<span class="input-group-addon input-group-addon-remove">
<span class="glyphicon glyphicon-remove"></span>
</span>
</div>
</div>
<div class="form-group form-group-options col-xs-11 col-md-3">
<div class="input-group input-group-option col-xs-12">
<input type="text" class="form-control" id="attendees" placeholder="Attendees">
<span class="input-group-addon input-group-addon-remove">
<span class="glyphicon glyphicon-remove"></span>
</span>
</div>
</div>
<div class="row no-print">
<div class="col-xs-12">
<input type="submit" value="Save" class="btn btn-primary pull-right">
</div>
</div>
</form>
my schema:
Emails = new Mongo.Collection('emails');
EmailSchema = new SimpleSchema({
"attendeesEmail": {
type: [Object],
optional: true
},
"attendeesEmail.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
ccEmails: {
type: [Object],
optional: true
},
"ccEmails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
}
});
Emails.attachSchema(EmailSchema);
event:
Template.form.events({
'submit .newMeeting': function(event) {
var cc = $('#cc').serializeArray();
var attendees = $('#attendees').serializeArray();
});
Insert:
Emails.insert({
attendeesEmail: [{address: attendees}],
ccEmails: [{address: cc }]
});
I’ve tried few solutions, but couldn’t get it working, any suggestions would be appreciated.