2016-12-20 8 views
0

私はユーザーの位置情報を画面に表示します。例についてはNode.jsでMongoDBを参照できません

:私は市や町の名前を表示したいgetuser関数を呼び出し

name: "Andy" surname : "Carol" City : "Istanbul" Town : "Kadıkoy" 

これは私のコードです:

UserSCHEMA

// Model for the User 
 
module.exports = (function userSchema() { 
 

 
    var Mongoose = require('mongoose'); 
 
    var Schema = Mongoose.Schema; 
 

 
    var userSchema = new Schema({ 
 

 
    name: { 
 
     type: String, 
 
     require: true 
 
    }, 
 
    surname: { 
 
     type: String, 
 
     require: true 
 
    }, 
 
    tel: { 
 
     type: String, 
 
     require: true 
 
    }, 
 
    age: { 
 
     type: String, 
 
     require: true 
 
    }, 
 
    mevki_id: { 
 
     type: String, 
 
     require: true 
 
    }, 
 
    lok_id: [{ 
 
     type: Mongoose.Schema.Types.ObjectId, 
 
     ref: 'locations' 
 
    }] 
 

 
    }); 
 

 
    var collectionName = 'users'; 
 
    var USERSCHEMA = Mongoose.Schema(userSchema); 
 
    var User = Mongoose.model(collectionName, USERSCHEMA); 
 

 
    return User; 
 

 
})();

UserControllerで

//This Controller deals with all functionalities of User 
function userController() { 

    var User = require('../models/UserSchema'); 

    // Creating New User 
    this.createUser = function (req, res, next) { 

    var name = req.body.name; 
    var surname = req.body.surname; 
    var tel = req.body.tel; 
    var age = req.body.age; 
    var mevki_id = req.body.mevki_id; 
    var lok_id = req.body.lok_id; 

    User.create({ 
     name: name, 
     surname: surname, 
     tel: tel, 
     age: age, 
     mevki_id: mevki_id, 
     lok_id: lok_id 
    }, function (err, result) { 
     if (err) { 
     console.log(err); 
     return res.send({ 
      'error': err 
     }); 
     } else { 
     return res.send({ 
      'result': result, 
      'status': 'successfully saved' 
     }); 
     } 
    }); 
    }; 

    //Populateeee 

    this.getUser = function (req, res, next) { 

    User.find().populate('lok_id') 
     .exec(function (err, result) { 
     if (err) { 
      console.log(err); 
      return res.send({ 
      'error': err 
      }); 
     } else { 
      return res.send({ 
      'USERS': result 
      }); 
     } 
     }); 
    }; 

    return this; 

}; 

module.exports = new UserController(); 

場所スキーマ

//Schema for Location 
module.exports = (function LocationSchema() { 

    var Mongoose = require('mongoose'); 
    var Schema = Mongoose.Schema; 

    var LocationSchema = new Schema({ 

    userid: { 
     type: Mongoose.Schema.Types.ObjectId, 
     ref: 'users' 
    }, 

    il: { 
     type: String, 
     require: true 
    }, 

    ilce: { 
     type: String, 
     require: true 
    } 

    }); 

    var collectionName = 'locations'; 
    var LocationSCHEMA = Mongoose.Schema(schema); 
    var Location = Mongoose.model(collectionName, LocationSCHEMA); 

    return Location; 
})(); 

場所コントローラ

//This Controller deals with all functionalities of Location 
function locationController() { 
    var location = require('../models/LocationSchema'); 

    // Creating New Location 
    this.createLocation = function (req, res, next) { 
    var userid = req.params.userid; 
    var il = req.params.il; 
    var ilce = req.params.ilce; 

    location.create({ 
     userid: userid, 
     il: il, 
     ilce: ilce 
    }, function (err, result) { 
     if (err) { 
     console.log(err); 
     return res.send({ 
      'error': err 
     }); 
     } else { 
     return res.send({ 
      'result': result, 
      'status': 'successfully saved' 
     }); 
     } 
    }); 
    }; 

    // Fetching Details of Location 
    this.getLocation = function (req, res, next) { 

    location.find({}, function (err, result) { 
     if (err) { 
     console.log(err); 
     return res.send({ 
      'error': err 
     }); 
     } else { 
     console.log(result); 
     return res.send({ 
      'location Details': result 
     }); 
     } 
    }); 
    }; 

    return this; 

}; 

module.exports = new locationController(); 
+0

場所スキーマ https://i.imgsafe.org/93f9ceb4fa.jpg LocationController https://i.imgsafe.org/93f9b72897.jpg – MAD

+0

含まれるように優れている見ます外部サイトに配置されたスクリーンショットではなくテキストとしてのコード。 –

+0

私は編集しました – MAD

答えて

0

は、私はすでにモデル定義に問題がありました。 それは

// Try to replace : 
 
var collectionName = 'users'; \t 
 
var USERSCHEMA=Mongoose.Schema(userSchema); 
 
var User = Mongoose.model(collectionName, USERSCHEMA); 
 
// with: 
 
var collectionName = 'users'; \t 
 
var USERSCHEMA=Mongoose.Schema(userSchema); 
 
var User = Mongoose.model(collectionName, USERSCHEMA, collectionName);
(明示的なコレクション名)mongoose.model第3のパラメータを追加することによって固定したCOLLECTIONNAMEはスキーマ定義またはモデル定義のいずれかで設定されなければなりません。以下のためのより多くの詳細が here

+0

あなたは正しく言うが、私の作成ユーザ機能は間違っているユーザコントローラでこの機能を見てもよろしいですか? – MAD

関連する問題