2016-12-21 40 views
0

您好我想在屏幕上看到用戶的所有信息,但我不能在MongoDB中添加用戶。在Schema.But我不知道如何獲取位置信息(即市,鎮...)輸入UserSchema.This位置的用戶的位置信息是我的代碼:我無法在mongodb中創建用戶,必須是功能錯誤嗎?

用戶模式:

var userSchema = Mongoose.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}, 
     location_id: { type: [Mongoose.Schema.Types.ObjectId], ref: 'locations' } 



}); 

Create User Function : 
this.createUser = function (req, res, next) { 
     var lok=new Location({il:req.params.il,ilce:req.params.ilce}); 
       lok.save(function(err){ 
    var user=new User({name:req.params.name,surname:req.params.surname,tel:req.params.tel,age:req.params.age,mevki_id:req.params.mevki_id,location_id:user}); 
     user.save(function(err){ 

      user.location_id=lok; 
       }); 
return res.send({}) 
       });  
       } 

Thnks: )

回答

0
//define this as your schema in your file 

var userSchema = new Schema({ 
    name: {type: String, required: true}, 
    surname: {type: String, required: true}, 
    tel: {type: Number, required: true}, 
    age: {type: Number, required: true}, 
    mevki_id: {type: String, required: true}, 
    location_id:{type:String,required:true} 
}); 
var CollectionModel_user = conn.model('users', userSchema) 

return function (req, res, next) { 
     req.Collection_user = CollectionModel_user; 
     next(); 
    } 

    // create a route which can create a route in your 
    // app.js or main.js file that will create a new 
    // collection in your mongo. 

    router.all('/user/create', function (req, res) { 
    var create = req.Collection_user; 
    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 location_id = req.body.location_id; 
    var record = new create({ 
     name: name, 
     surname: surname, 
     tel: tel, 
     age: age, 
     mevki_id: mevki_id, 
     location_id: location_id 
    }); 
    if (name.length > 0) { 
     record.save(function (err, result) { 
      if (err) { 
       res.json({status: 0, message: err}) 
      } else { 
       res.json({status: 1, message: " success"}); 
      } 
     }) 
    } else { 
     res.json({status: 0, msg: "Invalid Fields"}); 
    } 
}); 
+0

謝謝你的幫助,但它unfornutely沒有工作我想創建功能失常beause它在同一time.And創建兩個位置模式和userschema然後我得位置ID的定位模式,放入用戶模式,當用戶獲取用戶時,用戶具有位置信息了,但我不知道如何做到這一點:( – MAD

相關問題