1

I'm trying to use Simple Schema in my current Meteor React project but for some reason I can't get it to work.

This is my schema:

Comments.schema = new SimpleSchema({
  city: {
    type: String,
    label: 'The name of the city.'
  },

  person: {
    type: String,
    label: 'The name of the person.'
  },
  location: {
    type: String,
    label: 'The name of the location.'
  },
  title: {
    type: String,
    label: 'The title of the comment.'
  },

  content: {
  type: String,
  label: 'The content of the comment.'
  },

  fileLink: {
  type: String,
  regEx: SimpleSchema.RegEx.Url,
  label: 'The url of the file.'
  },

  createdBy: {
  type: String,
  autoValue: function(){ return this.userId },
  label: 'The id of the user.'
  }
});

And this is my insert:

  createSpark(event){
    event.preventDefault();

    const city = this.city.value;
    const person = this.person.value;
    const location = this.location.value;
    const title = this.title.value;
    const content = this.content.value;
    const fileLink = s3Url;

    insertComment.call({
      city, person, location, title, content, fileLink
      }, (error) => {
        if (error) {
              Bert.alert(error.reason, 'danger');
          } else {
              target.value = '';
              Bert.alert('Comment added!', 'success');
          }
      });
    }

I'm saving the value I get back from amazon in a global variable called s3Url. I am able to console.log this variable without a problem but when I want to write it to the database I am getting a "fileLink is not allowed by schema" error.

Anyone see what I am doing wrong?

Here is my comments.js file:

import faker from 'faker';
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Factory } from 'meteor/dburles:factory';

export const Comments = new Mongo.Collection('comments');

Comments.allow({
  insert: () => false,
  update: () => false,
  remove: () => false,
});

Comments.deny({
  insert: () => true,
  update: () => true,
  remove: () => true,
});

Comments.schema = new SimpleSchema({
  city: {
    type: String,
    label: 'The name of the city.'
  },

  person: {
    type: String,
    label: 'The name of the person.'
  },
  location: {
    type: String,
    label: 'The name of the location.'
  },
  title: {
    type: String,
    label: 'The title of the comment.'
  },

  content: {
    type: String,
    label: 'The content of the comment.'
  },

  fileLink: {
    type: String,
    regEx: SimpleSchema.RegEx.Url,
    label: 'The url of the file.'
  },

  createdBy: {
    type: String,
    autoValue: function(){ return this.userId },
    label: 'The id of the user.'
  }
});

Comments.attachSchema(Comments.schema);

And my methods.js file:

import { Comments } from './comments';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { rateLimit } from '../../modules/rate-limit.js';

export const insertComment = new ValidatedMethod({
  name: 'comments.insert',
  validate: new SimpleSchema({
    city: { type: String },
    person: { type: String, optional: true },
    location: { type: String, optional: true},
    title: { type: String },
    content: { type: String },
    fileLink: { type: String, regEx: SimpleSchema.RegEx.Url },
    createdBy: { type: String, optional: true }
  }).validator(),
  run(comment) {
    Comments.insert(comment);
  },
});

rateLimit({
  methods: [
    insertComment,

  ],
  limit: 5,
  timeRange: 1000,
});

While working a bit more on it I noticed some things I was doing wrong. 1. I didn't have the right value for my simple schema set up. 2. Some problems have to do with the fact the url has white spaces in it. What can I do to fix this? 3. The current error I am getting is: "Exception in delivering result of invoking 'comments.insert': ReferenceError: target is not defined."

Deelux
  • 1,045
  • 2
  • 12
  • 26
  • Is s3Url a full url or a relative path? Can you show the output of your console.log()? Also can you show the code in `insertComment.call()`? – Michel Floyd Nov 10 '16 at 15:50
  • @MichelFloyd thanks for your fast reply. I have continued to work on this and updated this post. It's still not working 100% but atleast I am getting an other error now. I think some problems also have to do with the fact the url contains white spaces. Any way to fix this? – Deelux Nov 12 '16 at 19:49
  • This is the url I store in s3Url: "https://ec2016.s3-eu-central-1.amazonaws.com/undefined/test.png" But when the file name contains spaces, it also doesn't work. – Deelux Nov 12 '16 at 19:50
  • Amazon should never give you a name with spaces. Also what is that trailing `"` at the end of your url. That looks wrong. Finally, your url probably needs an `http://` or `https://` prefix to pass `RegEx.url` – Michel Floyd Nov 13 '16 at 19:57
  • 1
    _Exception in delivering result of invoking 'comments.insert': ReferenceError: target is not defined_ this is obvious because you do not declare the `target` variable. I guess you are trying to clear the form element after submission, that would be done by `event.target.reset` – kkkkkkk Nov 15 '16 at 10:11
  • Thanks so much guys. It indeed had to do with the target variable that wasn't defined. It got me confused because I completely forgot about that variable being there. I thought it was referring to something else. Thanks again for taking the time to reply. – Deelux Nov 20 '16 at 20:08
  • I also managed to get the url formatted as I wanted by using: "encodeURI(s3Url)" – Deelux Nov 20 '16 at 20:10

1 Answers1

1

While working a bit more on it I noticed some things I was doing wrong. 1. I didn't have the right value for my simple schema set up. 2. Some problems have to do with the fact the url has white spaces in it. What can I do to fix this? 3. The current error I am getting is: "Exception in delivering result of invoking 'comments.insert': ReferenceError: target is not defined."

Thanks @Khang

Deelux
  • 1,045
  • 2
  • 12
  • 26