【发布时间】:2016-11-30 00:57:08
【问题描述】:
我正在尝试在我当前的 Meteor React 项目中使用 Simple 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.'
}
});
这是我的插入:
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');
}
});
}
我将从亚马逊返回的值保存在一个名为 s3Url 的全局变量中。我可以毫无问题地 console.log 这个变量,但是当我想将它写入数据库时,我收到“模式不允许 fileLink”错误。
有人看到我做错了吗?
这是我的 cmets.js 文件:
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);
还有我的 methods.js 文件:
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,
});
在做更多的工作时,我注意到我做错了一些事情。 1. 我的简单模式设置没有正确的值。 2. 一些问题与网址中有空格有关。我能做些什么来解决这个问题? 3.我得到的当前错误是:“在传递调用'cmets.insert'的结果时出现异常:ReferenceError:未定义目标。”
【问题讨论】:
-
s3Url 是完整的 url 还是相对路径?你能显示你的 console.log() 的输出吗?你也可以在
insertComment.call()中显示代码吗? -
@MichelFloyd 感谢您的快速回复。我继续为此工作并更新了这篇文章。它仍然不能 100% 工作,但至少我现在遇到了另一个错误。我认为一些问题也与 url 包含空格的事实有关。有什么办法解决这个问题?
-
这是我存储在s3Url中的url:"ec2016.s3-eu-central-1.amazonaws.com/undefined/test.png" 但是当文件名中包含空格时,它也不起作用。
-
亚马逊不应该给你一个带空格的名字。还有你网址末尾的
"是什么。那看起来不对。最后,您的 url 可能需要一个http://或https://前缀来传递RegEx.url -
传递调用 'cmets.insert' 的结果时出现异常:ReferenceError: target is not defined 这很明显,因为您没有声明
target变量。我猜你想在提交后清除表单元素,这将由event.target.reset完成
标签: javascript reactjs meteor simple-schema