2016-12-21 8 views
0

привет, я хочу видеть на экране пользователя всю информацию, но я не могу добавить пользователя в mongodb. Информация о местоположении пользователя в Locations 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 он должен создать и схему расположения и userschema в то же time.And то я должен получить идентификатор местоположения в схеме расположения, поместить в пользовательскую схему, а когда ı cal получить пользователя, у пользователя есть информация о местоположении больше, но я не знаю, как это сделать :( – MAD