Inserting array of strings into collection2


#1

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.


#2

Check out this doc: https://github.com/aldeed/meteor-simple-schema#type

If you are trying to insert an array of strings, you would use type: [String]


#3

@vigorwebsolutions thanks for suggestions. That was the first thing i’ve tried and it kept throwing an error insert failed: Error: 0 must be a string


#4

Well a quick look at serializeArray says that you’ve actually got an array of objects, which would be type: [Object] then.


#5

when I use [Object] instead of [Array] it throws an error insert failed: Error: Address must be a string


#6

Why not skip the schema validation until you can see the actual value (and type of value) that is being stored in your db?


#7

tried doing this

Emails.insert({
attendeesEmail: [{address: attendees}],
ccEmails: [{address: cc }],
validate: false
});

according to collection2 docs, but it didnt work, as browser console kept throwing error insert failed: Error: Address must be a string


#8

Can you show us the output of:

console.log(attendees);

#9

@hwillson the output is an empty array []


#10

Okay - given that your code is really trying to do the following:

Emails.insert({
  attendeesEmail: [{ address: [ ] }],
  ...
});

but your schema says:

...
'attendeesEmail.$.address': {
  type: String,
  regEx: SimpleSchema.RegEx.Email
},
...

You’re saying the type of address should be a String, but you’re passing in an Array. Hence the insert failed: Error: Address must be a string. As @vigorwebsolutions mentioned, try changing your type to [String].


#11

@hwillson Ive tried that already, and it throws an error insert failed: Error: 0 must be a string and the console log prints [object]


#12

Okay - keep the type as [String] and now remove your regEx: SimpleSchema.RegEx.Email line.

Just to add - for an explanation of what’s happening, check out:


#13

I’ve removed regEx: SimpleSchema.RegEx.Email, and the result is the same. Same error, same output on console