meteorsimple-schemameteor-collection2

Saving API call result to a collection but recieving errors while setting up a SimpleSchema


I have a Meteor method which does an API call, then the response of the call is saved to the users collection. I'm using the Collection2 package with my project and I'm a little bit lost setting up my SimpleSchema for this.

Here is what the JSON response looks like from the API call:

[{"keyword":"2i","url":"http://example.com","title":"Example","timestamp":"2016-11-05 08:54:42","ip":"00.00.00.000","clicks":"2","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"2j","url":"http://example.com","title":"YouTube","timestamp":"2016-11-06 02:11:18","ip":"00.00.00.000","clicks":"1","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"2k","url":"http://example.com","title":"YouTube","timestamp":"2016-11-08 03:35:12","ip":"00.00.00.000","clicks":"0","user":"HweoSCY2ujscjJ9Zl"}]

Here's currently how I've been able to save this data to the users collection:

Meteor.users.update(Meteor.userId(), {
  $set: { 'shortURLs.URLS': result.data }
});

This works and looks like this in the db:

screenshot

My issue is that I'd like to have a SimpleSchema setup for this so that the "timestamp" will be saved as a Date instead of a String, but everytime I try and create a schema for it I just receive errors like "After filtering out keys not in the schema, your modifier is now empty". I have tried a lot of different variations to try and make it work but none of them have been successful, here's just currently where it's at:

Schema.ShortURLs = new SimpleSchema({
    shortURLs: {
        type: Object
    },
    'shortURLs.$': {
        type: Object
    },
    'shortURLs.$.keyword': {
        type: String,
        optional: true,
        label: "Keyword"
    },
    'shortURLs.$.url': {
        type: String,
        optional: true,
        label: "URL"
    },
    'shortURLs.$.title': {
        type: String,
        optional: true,
        label: "Title"
    },
    'shortURLs.$.timestamp': {
        type: Date,
        optional: true,
        label: "Timestamp"
    },
    'shortURLs.$.ip': {
        type: String,
        optional: true,
        label: "IP"
    },
    'shortURLs.$.clicks': {
        type: String,
        optional: true,
        label: "Clicks"
    },
    'shortURLs.$.user': {
        type: String,
        optional: true,
        label: "User"
    },
});

This is then attached apart of a User Simple Schema:

...
shortURLs: {
    type: Schema.ShortURLs,
    optional: true
},
...

And I have that attached to the users collection:

Meteor.users.attachSchema(Schema.User);

I don't think there's an issue with how I have it attached as I have other SimpleSchemas setup the same way and they're working fine, I believe the issue is how I have this particular one written. Any help here would be extremely appreciated.


Solution

  • You need to define the shortURLs type in the Meteor.users collection as type: [Schema.ShortURLs], i.e. a list of type Schema.ShortURLs

    Schema = {}
    Schema.ShortURLs = new SimpleSchema({
        keyword: {
            type: String,
            optional: true,
            label: "Keyword"
        },
        url: {
            type: String,
            optional: true,
            label: "URL"
        },
        title: {
            type: String,
            optional: true,
            label: "Title"
        },
        timestamp: {
            type: Date,
            optional: true,
            label: "Timestamp"
        },
        ip: {
            type: String,
            optional: true,
            label: "IP"
        },
        clicks: {
            type: String,
            optional: true,
            label: "Clicks"
        },
        user: {
            type: String,
            optional: true,
            label: "User"
        },
    });
    
    Schema.User = new SimpleSchema({
    ...
      shortURLs: {
          type: [Schema.ShortURLs],
          optional: true
      },
    ...
    });
    
    ...
    
    Meteor.users.attachSchema(Schema.User);