Javascript 模拟调用'的效果时发生异常;交易。插入';错误
我正在尝试使用react和meteor将表单数据提交到数据库 我有一个用于表单的AddDeal组件和一个用于交易的集合,以及其中的一个方法 错误 模拟调用“deals.insert”的效果时发生异常 ReferenceError:\u id未定义 获取错误:单击提交时需要ID 我不知道插入时如何处理_id 这是我的代码,谢谢你的帮助 onSubmit(e)函数Javascript 模拟调用'的效果时发生异常;交易。插入';错误,javascript,mongodb,reactjs,meteor,Javascript,Mongodb,Reactjs,Meteor,我正在尝试使用react和meteor将表单数据提交到数据库 我有一个用于表单的AddDeal组件和一个用于交易的集合,以及其中的一个方法 错误 模拟调用“deals.insert”的效果时发生异常 ReferenceError:\u id未定义 获取错误:单击提交时需要ID 我不知道插入时如何处理_id 这是我的代码,谢谢你的帮助 onSubmit(e)函数 onSubmit(e) { e.preventDefault(); const title = this.stat
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.\u 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从insert和通过简单模式进行的验证中去掉,然后插入了:)这个答案是错误的
this.userId
在方法中是方法上下文的一部分,并映射到调用用户的id。@MasterAM那么我该如何更改它呢?正如OP所提到的,从某个表达式中去掉\u id
会导致错误消失。OP的代码仍然不完整且有缺陷,因为ht/she没有检查输入,并以错误的顺序发送参数,但在任何情况下,它都与this.userId
无关。