简体   繁体   中英

Mongo collection in Meteor is inserting junk

I'm trying to create a Mongo collection that can be inserted into from client side. When I call Courses.insert it succeeds, but is putting junk fields into my collection.

Code:

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

Once I call Courses.insert(Meteor.userId(), toInsert); I open up a console and view the contents of the collection with db.courses.find()

The meteor console output is:

{ "_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" }

Note: Everytime I insert into the database, the _id is different, but the other key:values are all identical no matter what data I attempt to insert.

The javascript console output is:

Course Form Submitted.
createCourse.js:33 about to insert
createCourse.js:35 NvcBX7MnSMx2LyJFz

Can someone please help to point out what the issue is. I would also appreciate any feedback on secure practices regarding collections and any feedback in general.

There is no need to pass the userId explicitly to insert. The insert function is documented in Meteor API . The junk that you see is the userId.

Just use:

var result = Courses.insert(toInsert);

Remove the Meteor.userId() from the insert query. Just insert like this:

var result = Courses.insert(toInsert);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM