2016-08-15 6 views
2

を作成しながら、私はマングースモデルを作成しようとするMongoDBからデータを取得するためにそれを使用して、私は例外をパッケージからマングースモデル

マイマングースのバージョンを取得していています未定義のプロパティ「ユーザ」を読み取ることができませんガイド.jsonは次のとおりです。"マングース": "4.5.5"

TypeError: Cannot read property 'users' of undefined 

が今ここに私のデモコードは次のとおりです。

var mongoose = require("mongoose"); 
var schema = mongoose.Schema; 
var Users = { 
    _id   : { 
     type  : String, 
     require  : true 
    }, 
    username : { 
     type  : String, 
     required : true 
    }, 
    password : { 
     type  : String, 
     required : true 
    } 
}; 
var UsersModel = new mongoose.model('users', Users); 
var newUser = new UsersModel({"username":"A", "password":"V"}); 
newUser.save(function(err, result){ 
    if(err){ 
     console.log('>>>>>> Error'); 
    }else{ 
     console.log('>>>>>> ' + JSON.stringify(result, null, 4)); 
    } 
}); 

そして、ここではエラーです:

/home/ankur/Private/Work/ps/node_modules/mongoose/lib/index.js:343 
if (!this.modelSchemas[name]) {                   
         ^                    
TypeError: Cannot read property 'users' of undefined              
    at new Mongoose.model (/home/ankur/Private/Work/ps/node_modules/mongoose/lib/index.js:343:25)   
    at Object.<anonymous> (/home/ankur/Private/Work/ps/models_mongoose/Users.model.js:24:21)    
    at Module._compile (module.js:456:26)                 
    at Object.Module._extensions..js (module.js:474:10)             
    at Module.load (module.js:356:32)                  
    at Function.Module._load (module.js:312:12)               
    at Function.Module.runMain (module.js:497:10)               
    at startup (node.js:119:16)                   
    at node.js:929:3     

私の質問はこのコードにあります。余計なことはほとんどありません。スキーマを設定し、そのスキーマで新しいモデルを作成して保存しました。 だから私はものを逃した?

いくつかの意見を控えてください。

+0

私は 'var Users = {...}'は 'var Users = mongoose.Schema({...}) 'であるべきだと思います。それを試しましたか? – mscdex

+0

まだ同じです。 –

答えて

2

エラーはほとんどありません。私は完全に動作する例を添付しました:

var mongoose = require("mongoose"); 
mongoose.connect('mongodb://localhost:27017/db_name'); //you need open to connection - this is an example of valid connection string 

var Schema = mongoose.Schema; 
var Users = new Schema({ // example from docs 
    _id   : { 
     type  : String, 
     require  : true 
    }, 
    username : { 
     type  : String, 
     required : true 
    }, 
    password : { 
     type  : String, 
     required : true 
    } 
}); 
var UsersModel = mongoose.model('users_test', Users); //remove 'new' keyword 
var newUser = new UsersModel({"username":"A", "password":"V", "_id": 0}); // you also need here to define _id since, since you set it as required. 
newUser.save(function(err, result){ 
    if(err){ 
     console.log('>>>>>> Error'); 
    }else{ 
     console.log('>>>>>> ' + JSON.stringify(result, null, 4)); 
    } 
}); 

まとめると:

  1. あなたはすべきmongooseオブジェクトにconnectメソッドを呼び出して、接続を作成する
  2. 使用はnew Schemaコンストラクタモデル
  3. 削除を作成中newキーワードからvar UsersModel = mongoose.model(...)
  4. 値を持つ_idキーを追加します。bこの値が足りないというエラーが発生することはありません。
1

スキーマとモデルを混在させました。

var mongoose = require("mongoose"); 
var Schema = mongoose.Schema; 
var Users = new Schema({ // add schema here! 
    _id   : { 
    type  : String, 
    require  : true 
    }, 
    username : { 
    type  : String, 
    required : true 
    }, 
    password : { 
    type  : String, 
    required : true 
    } 
}); 
var UsersModel = mongoose.model('users', Users); // remove new here! 
var newUser = new UsersModel({"username":"A", "password":"V"}); 
+0

ありがとう、試してみましょう... :) +1 –

関連する問題