【问题标题】:Exception while simulating the effect of invoking 'deals.insert' error模拟调用“deals.insert”错误的效果时出现异常
【发布时间】:2017-09-27 13:00:36
【问题描述】:

我正在尝试使用 react 和流星将表单数据提交到数据库。

我有一个用于表单的 AddDeal 组件和一个用于交易的集合以及其中的一个方法。

错误

模拟调用“deals.insert”的效果时出现异常 ReferenceError: _id 未定义

得到错误:点击提交时需要ID。

插入时不知道怎么处理_id。

这是我的代码,感谢您的帮助!

onSubmit(e) 函数

  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'
    });
  }

插入方法

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
    });
  }
});

【问题讨论】:

    标签: javascript mongodb reactjs meteor


    【解决方案1】:

    deals.insert 上,您正在验证参数this.userId 而不是this._id

    我认为你需要改变这一点:

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

    对此:

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

    【讨论】:

    • 谢谢 :) 我也通过将 _id 从插入中取出并通过简单模式进行验证并将其插入:)
    • 这个答案不正确。方法中的this.userId 是方法上下文的一部分,并映射到调用用户的 id。
    • @MasterAM 那我该如何改变呢?
    • 正如 OP 所提到的,从某些表达式中取出 _id 会导致错误消失。 OP 的代码仍然不完整且有问题,因为 ht/she 不检查输入,并且以错误的顺序发送参数,但无论如何,它与 this.userId 无关。
    猜你喜欢
    • 2015-06-17
    • 2016-09-23
    • 2015-11-13
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多