gpt4 book ai didi

javascript - 模拟调用 'deals.insert'错误效果时出现异常

转载 作者:行者123 更新时间:2023-12-03 04:26:26 25 4
gpt4 key购买 nike

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

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

错误

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

出现错误:单击提交时需要 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
});
}
});

最佳答案

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

关于javascript - 模拟调用 'deals.insert'错误效果时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43696633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com