2016-07-27 3 views
0

クライアント側から挿入できるMongoコレクションを作成しようとしています。私がCourses.insertを呼び出すと、それは成功しますが、私のコレクションにジャンクフィールドを入れています。MeteorのMongoコレクションがジャンクを挿入しています

コード:

/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); Iコンソールを開き、コレクションの内容をで表示する0

流星コンソール出力は次のとおりです。

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

誰かが問題が何であるかを指摘して助けてください。また、コレクションに関する安全な慣行やフィードバックについてのご意見をお待ちしております。

答えて

1

挿入するために明示的にuserIdを渡す必要はありません。挿入機能については、Meteor APIに記載されています。あなたが見るジャンクはuserIdです。

ちょうど使用:

var result = Courses.insert(toInsert); 
+0

ありがとうございます。私がuserId()を使用していた理由は、「挿入」機能(userId)のためです。もともとは「挿入」でした:function(userId、doc)。しかし、私は正確にこの機能が何を理解していないのですか?説明できますか? –

0

は(Meteor.userIdを削除)INSERTクエリから。

var result = Courses.insert(toInsert); 
関連する問題