我正在尝试创建一个可以从客户端插入的Mongo集合。当我调用Courses.insert时它成功了,但是将垃圾字段放入我的集合中。
代码:
/imports/api/createCourse.js
import { Meteor } from 'meteor/meteor'
import { Template } from 'meteor/templating';
import './createCourse.html'
import { Courses } from './collections.js'
if (Meteor.isClient) {
Template.createCourse.events({
'submit #register_form' : function(event) {
var cName = event.target.courseName.value;
var aCode = event.target.accessCode.value;
var aClosedDate = event.target.accessClosedDate.value;
console.log("Course Form Submitted.");
//var cID = new ObjectID;
var toInsert = {
courseID: 1,
ownerID: Meteor.userId(),
courseName: cName,
restrictionMask: 0, //????
accessCode: aCode,
accessClosedDate: aClosedDate,
disabled: false
};
// Courses.schema.validate(toInsert);
console.log("about to insert");
var result = Courses.insert(Meteor.userId(), toInsert);
console.log(result);
}
})
}
/imports/api/collections.js
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import 'meteor/aldeed:collection2';
export const Courses = new Mongo.Collection("courses");
Courses.allow({
'insert': function (userId) {
return true;
}
})
拨打Courses.insert(Meteor.userId(), toInsert);
后,我打开一个控制台,用db.courses.find()
流星控制台输出是:
{ "_id" : "NvcBX7MnSMx2LyJFz", "0" : "m", "1" : "X", "2" : "3", "3" : "H", "4" : "m", "5" : "i", "6" : "C", "7" : "p
", "8" : "p", "9" : "f", "10" : "a", "11" : "H", "12" : "e", "13" : "v", "14" : "9", "15" : "7", "16" : "R" }
注意:每次插入数据库时,_id都不同,但其他键:无论我尝试插入什么数据,值都是相同的。
javascript控制台输出是:
Course Form Submitted.
createCourse.js:33 about to insert
createCourse.js:35 NvcBX7MnSMx2LyJFz
有人可以帮助指出问题所在。我也非常感谢有关收藏的安全做法和一般反馈的任何反馈。
答案 0 :(得分:1)
答案 1 :(得分:0)
从插入查询中删除Meteor.userId()。只需插入如下:
var result = Courses.insert(toInsert);