cala
cala

Reputation: 1421

Exception while simulating the effect of invoking 'deals.insert' error

I'm trying to submit form data to the database with react and meteor.

I have a AddDeal component for the form and a collection for the deals and also a method inside it.

Error

Exception while simulating the effect of invoking 'deals.insert' ReferenceError: _id is not defined

Getting the error: ID is required when submit is clicked.

I don't know how to handle the _id when inserting.

Here is my code, and thanks for helping!

onSubmit(e) function

  onSubmit(e) {
    e.preventDefault();

    const title = this.state.title.trim();
    const description = this.state.description;
    const category = this.state.category;
    const location = this.state.location;
    const price = this.state.price.trim();

    e.preventDefault();

    if (title, description, category, location, price) {
      Meteor.call('deals.insert', title, description, category, location, price);
    }

    alert('Title is: ' + this.state.title + 'Description is: ' + this.state.description + 'Category is: ' + this.state.category
          + 'Location is: ' + this.state.location + 'Price: ' + this.state.price);

    this.setState({
      title: '',
      description: '',
      category: 'technology',
      location: 'USA',
      price: '0.00'
    });
  }

Insert method

export const Deals = new Mongo.Collection('deals');

if (Meteor.isServer) {
  Meteor.publish('deals', function () {
    return Deals.find({ userId: this.userId });
  });
}

Meteor.methods({
  'deals.insert'(_id, title, description, category, price, location) {
    if (!this.userId) {
      throw new Meteor.Error('not-allowed');
    }

    new SimpleSchema({
      _id: {
        type: String,
        min: 1
      },
      title: {
        type: String,
        optional: true
      },
      description: {
        type: String,
        optional: true
      },
      category: {
        type: String,
        optional: true
      },
      location: {
        type: String,
        optional: true
      },
      price: {
        type: Number,
        optional: true
      }
    }).validate({

    });

    Deals.insert({
      _id,
      title,
      description,
      category,
      location,
      price,
      createdAt: Date(),
      userId: this.userId
    });
  }
});

Upvotes: 0

Views: 264

Answers (1)

Sagiv b.g
Sagiv b.g

Reputation: 31024

On deals.insert you are validating the parameter this.userId instead of this._id?

I think you nedd to change this:

'deals.insert'(_id, title, description, category, price, location) {
    if (!this.userId) {
      throw new Meteor.Error('not-allowed');
    }
...

to this:

'deals.insert'(_id, title, description, category, price, location) {
    if (!this._id) {
      throw new Meteor.Error('not-allowed');
    }

Upvotes: 1

Related Questions