Adding comments in meteor. Error : 'insert failed'
Adding comments in meteor. Error : 'insert failed'
过去几个小时我一直在苦思冥想。我知道我犯了一个愚蠢的错误,但我似乎无法让代码正常工作。我不断收到 insert failed: Error: fritkotId is required
错误。任何帮助将不胜感激。
我正在尝试将评论功能添加到我的一个对象中。这就是我现在拥有的...
# collections/comments.js
// Schema Definitions
CommentSchema = new SimpleSchema({
body: {
type: String,
label: 'Body'
},
fritkot: {
type: String,
label: 'fritkotId',
autoValue: function() {
return Session.get('fritkotId', this._id)
},
autoform: {
type: 'hidden'
}
},
author: {
type: String,
label: 'User',
autoValue: function() {
return this.userId
},
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: 'Created At',
autoValue: function() {
return new Date()
},
autoform: {
type: 'hidden'
}
}
});
# collections/fritkots.js
import './comments.js
// Schema Definitions
FritkotSchema = new SimpleSchema({
comments: {
type: Array,
optional: true
},
'comments.$': {
type: Object
},
'comment.$.body': {
type: String
},
fav: {
type: Boolean,
defaultValue: false,
optional: true,
autoform: {
type: 'hidden'
}
},
name: {
type: String,
label: 'Name'
},
address: {
type: String,
label: 'Address',
},
postCode: {
type: String,
label: 'PostCode'
},
gemeente: {
type: String,
label: 'Gemeente'
},
info: {
type: String,
label: 'Description'
},
author: {
type: String,
label: 'User',
autoValue: function() {
return this.userId
},
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: 'Created At',
autoValue: function() {
return new Date()
},
autoform: {
type: 'hidden'
}
}
});
fritkotSingle.js
import './fritkotSingle.tpl.jade' // Single Fritkot View Page
// On Creation
Template.fritkotSingle.onCreated(function() {
let self = this;
self.autorun(() => {
let fritkotId = FlowRouter.getParam('fritkotId');
self.subscribe('singleFritkot', fritkotId);
})
});
// Helpers
Template.fritkotSingle.helpers({
fritkot: () => {
let fritkotId = FlowRouter.getParam('fritkotId');
let fritkot = Fritkots.findOne( { _id: fritkotId } ) || {};
return fritkot
},
editable: function () {
return Session.equals('editFritkotId', true)
},
// comments: function() {
// // let fritkot = Fritkots.findOne( { id: Session.get(this._id)} );
// let fritkot = Fritkots.findOne( { _id: this._id } );
// return fritkot.comments
// },
commentsCount: function() {
return Comments.find( { fritkotId: this._id} ).count();
console.log(`There are ${Comments.find( { fritkotId: this._id} ).count()} comments written by this user`);
}
// comments: () => {
// look up comments that have matching fritkotId
// loop thru comments
// }
});
fritkotSingle.tpl.jade
with fritkot
if editable
+editFritkot
else
.section.section--single
article.card.card--single
h3.card__title= name
p.card__content{{commentsCount}} comments
p.card__content= address
p.card__content
= gemeente
span.card__content= postCode
p.card__info= info
if fav
button.card__btn.btn--isDenied.toggle-fav Remove from Favorites
else
button.card__btn.btn--isAllow.toggle-fav Add to Favorites
//- button.card__btn.toggle-fav Add to Favorites
button.card__btn.form-delete Delete
//- button.card__btn.btn--isEdit.form-edit Edit
button.card__btn.form-edit Edit
.section.section--comments
+comments
newComment.js
import './newComment.tpl.jade'
Template.newComment.events({
'click .form-save': (e) => {
e.preventDefault();
let body = $('.new-comment').val();
Comments.insert({
// check(id, String);
body: body,
createdAt: new Date()
});
console.log('A comment was inserted with this text: ' + body);
Session.set('newComment', false);
// FlowRouter.go('fritkotSingle');
// console.log('redirecting');
},
'click .form-close': (e) => {
e.preventDefault();
Session.set('newComment', false);
}
})
我认为我的问题要么出在我的架构定义上,要么出在我在 newComment
表单上调用插入的方式上。我需要以某种方式将 fritkotId
传递给我的评论并在我保存时将其插入,但我在定义它的位置时遇到了问题。
Meteor 的 Session.get(key)
方法只接受一个参数。看来您正在为 fritkot.autoValue
条目的模式中的 Session.get()
方法传递两个参数:
fritkot: {
type: String,
label: 'fritkotId',
autoValue: function() {
return Session.get('fritkotId', this._id)
},
autoform: {
type: 'hidden'
}
},
您最有可能想做的事情:
fritkot: {
type: String,
label: 'fritkotId',
autoValue: Session.get('fritkotId'),
autoform: {
type: 'hidden'
}
},
编辑:所以问题出在我的 collection/comments.js
上。显然,在 SimpleSchema
和调用 Comments.insert
中声明不同的字段不允许我使用 fritkotId
添加评论。
我暂时把 SimpleSchema
定义注释掉了。
这是实际有效的代码。
Comments = new Mongo.Collection('comments')
// Permissions
Comments.allow({
insert: (userId, doc) => {
// return !! userId;
return true
},
update: (userId, doc) => {
return !! userId;
},
remove: (userId, doc) => {
return !! userId;
}
});
// Schema Definitions
// CommentSchema = new SimpleSchema({
// body: {
// type: String,
// label: 'Body',
// min: 1,
// max: 140
// },
// author: {
// type: String,
// label: 'User',
// autoValue: function() {
// return this.userId
// },
// autoform: {
// type: 'hidden'
// }
// },
// createdAt: {
// type: Date,
// label: 'Created At',
// autoValue: function() {
// if (this.isInsert) {
// return new Date()
// } else if (this.isUpsert) {
// return { $setOnInsert: new Date }
// } else {
// this.unset()
// }
// },
// autoform: {
// type: 'hidden'
// }
// },
// lastUpdatedAt: {
// type: Date,
// autoValue: function() {
// if (this.isUpdate) {
// return new Date ()
// }
// },
// denyInsert: true,
// optional: true
// }
// });
// Methods
Meteor.methods({
// updateComment: (id) => {
// Comments.update(id, {
// $set: { text: }
// }),
createComment: function (commentContent, fritkotId) {
check(commentContent, String);
check(fritkotId, String);
let currentUserId = Meteor.userId();
if(currentUserId && fritkotId) {
Comments.insert({
body: commentContent,
author: currentUserId,
fritkotId: fritkotId,
createdAt: new Date()
});
} else {
throw new Meteor.Error('you must comment on a fritkot');
}
},
updateComment: (id) => {
// Session.set('editComment', true);
},
deleteComment: (id) => {
Comments.remove(id);
}
})
// Comments.attachSchema( CommentSchema );
不,我需要让它与 SimpleSchema
一起使用
过去几个小时我一直在苦思冥想。我知道我犯了一个愚蠢的错误,但我似乎无法让代码正常工作。我不断收到 insert failed: Error: fritkotId is required
错误。任何帮助将不胜感激。
我正在尝试将评论功能添加到我的一个对象中。这就是我现在拥有的...
# collections/comments.js
// Schema Definitions
CommentSchema = new SimpleSchema({
body: {
type: String,
label: 'Body'
},
fritkot: {
type: String,
label: 'fritkotId',
autoValue: function() {
return Session.get('fritkotId', this._id)
},
autoform: {
type: 'hidden'
}
},
author: {
type: String,
label: 'User',
autoValue: function() {
return this.userId
},
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: 'Created At',
autoValue: function() {
return new Date()
},
autoform: {
type: 'hidden'
}
}
});
# collections/fritkots.js
import './comments.js
// Schema Definitions
FritkotSchema = new SimpleSchema({
comments: {
type: Array,
optional: true
},
'comments.$': {
type: Object
},
'comment.$.body': {
type: String
},
fav: {
type: Boolean,
defaultValue: false,
optional: true,
autoform: {
type: 'hidden'
}
},
name: {
type: String,
label: 'Name'
},
address: {
type: String,
label: 'Address',
},
postCode: {
type: String,
label: 'PostCode'
},
gemeente: {
type: String,
label: 'Gemeente'
},
info: {
type: String,
label: 'Description'
},
author: {
type: String,
label: 'User',
autoValue: function() {
return this.userId
},
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: 'Created At',
autoValue: function() {
return new Date()
},
autoform: {
type: 'hidden'
}
}
});
fritkotSingle.js
import './fritkotSingle.tpl.jade' // Single Fritkot View Page
// On Creation
Template.fritkotSingle.onCreated(function() {
let self = this;
self.autorun(() => {
let fritkotId = FlowRouter.getParam('fritkotId');
self.subscribe('singleFritkot', fritkotId);
})
});
// Helpers
Template.fritkotSingle.helpers({
fritkot: () => {
let fritkotId = FlowRouter.getParam('fritkotId');
let fritkot = Fritkots.findOne( { _id: fritkotId } ) || {};
return fritkot
},
editable: function () {
return Session.equals('editFritkotId', true)
},
// comments: function() {
// // let fritkot = Fritkots.findOne( { id: Session.get(this._id)} );
// let fritkot = Fritkots.findOne( { _id: this._id } );
// return fritkot.comments
// },
commentsCount: function() {
return Comments.find( { fritkotId: this._id} ).count();
console.log(`There are ${Comments.find( { fritkotId: this._id} ).count()} comments written by this user`);
}
// comments: () => {
// look up comments that have matching fritkotId
// loop thru comments
// }
});
fritkotSingle.tpl.jade
with fritkot
if editable
+editFritkot
else
.section.section--single
article.card.card--single
h3.card__title= name
p.card__content{{commentsCount}} comments
p.card__content= address
p.card__content
= gemeente
span.card__content= postCode
p.card__info= info
if fav
button.card__btn.btn--isDenied.toggle-fav Remove from Favorites
else
button.card__btn.btn--isAllow.toggle-fav Add to Favorites
//- button.card__btn.toggle-fav Add to Favorites
button.card__btn.form-delete Delete
//- button.card__btn.btn--isEdit.form-edit Edit
button.card__btn.form-edit Edit
.section.section--comments
+comments
newComment.js
import './newComment.tpl.jade'
Template.newComment.events({
'click .form-save': (e) => {
e.preventDefault();
let body = $('.new-comment').val();
Comments.insert({
// check(id, String);
body: body,
createdAt: new Date()
});
console.log('A comment was inserted with this text: ' + body);
Session.set('newComment', false);
// FlowRouter.go('fritkotSingle');
// console.log('redirecting');
},
'click .form-close': (e) => {
e.preventDefault();
Session.set('newComment', false);
}
})
我认为我的问题要么出在我的架构定义上,要么出在我在 newComment
表单上调用插入的方式上。我需要以某种方式将 fritkotId
传递给我的评论并在我保存时将其插入,但我在定义它的位置时遇到了问题。
Meteor 的 Session.get(key)
方法只接受一个参数。看来您正在为 fritkot.autoValue
条目的模式中的 Session.get()
方法传递两个参数:
fritkot: {
type: String,
label: 'fritkotId',
autoValue: function() {
return Session.get('fritkotId', this._id)
},
autoform: {
type: 'hidden'
}
},
您最有可能想做的事情:
fritkot: {
type: String,
label: 'fritkotId',
autoValue: Session.get('fritkotId'),
autoform: {
type: 'hidden'
}
},
编辑:所以问题出在我的 collection/comments.js
上。显然,在 SimpleSchema
和调用 Comments.insert
中声明不同的字段不允许我使用 fritkotId
添加评论。
我暂时把 SimpleSchema
定义注释掉了。
这是实际有效的代码。
Comments = new Mongo.Collection('comments')
// Permissions
Comments.allow({
insert: (userId, doc) => {
// return !! userId;
return true
},
update: (userId, doc) => {
return !! userId;
},
remove: (userId, doc) => {
return !! userId;
}
});
// Schema Definitions
// CommentSchema = new SimpleSchema({
// body: {
// type: String,
// label: 'Body',
// min: 1,
// max: 140
// },
// author: {
// type: String,
// label: 'User',
// autoValue: function() {
// return this.userId
// },
// autoform: {
// type: 'hidden'
// }
// },
// createdAt: {
// type: Date,
// label: 'Created At',
// autoValue: function() {
// if (this.isInsert) {
// return new Date()
// } else if (this.isUpsert) {
// return { $setOnInsert: new Date }
// } else {
// this.unset()
// }
// },
// autoform: {
// type: 'hidden'
// }
// },
// lastUpdatedAt: {
// type: Date,
// autoValue: function() {
// if (this.isUpdate) {
// return new Date ()
// }
// },
// denyInsert: true,
// optional: true
// }
// });
// Methods
Meteor.methods({
// updateComment: (id) => {
// Comments.update(id, {
// $set: { text: }
// }),
createComment: function (commentContent, fritkotId) {
check(commentContent, String);
check(fritkotId, String);
let currentUserId = Meteor.userId();
if(currentUserId && fritkotId) {
Comments.insert({
body: commentContent,
author: currentUserId,
fritkotId: fritkotId,
createdAt: new Date()
});
} else {
throw new Meteor.Error('you must comment on a fritkot');
}
},
updateComment: (id) => {
// Session.set('editComment', true);
},
deleteComment: (id) => {
Comments.remove(id);
}
})
// Comments.attachSchema( CommentSchema );
不,我需要让它与 SimpleSchema