猫鼬人口不起作用?

时间:2017-03-27 06:52:35

标签: mongodb express mongoose

我是新手意味着我想使用populate方法将集合引用到另一个集合,但无法这样做

公司架构

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ClientEmployee  = require('../models/clientEmployee')

var CompanySchema = new Schema({
companyId : {type: String, required  :true, unique : true},
companyName : {type: String, lowercase : true, required  :true, unique : 
true},
phone : {type: String},
mobile :  {type: String},
website : {type: String, lowercase : true, required  :true, unique : true},
address : {type: String},
city : {type: String},
zipcode : {type: String},
country : {type: String},
vatNumber : {type: String},
primaryContacts : [{ type: Schema.Types.ObjectId, ref: 'ClientEmployee'}]
})

module.exports = mongoose.model('Company',CompanySchema)

我正在尝试将clientemployee填充到公司

ClientEmployee架构

var ClientContactSchema = new Schema({
     var mongoose = require('mongoose');
     var Schema = mongoose.Schema;
     var ClientEmployeeSchema = new Schema({
     username : {type: String, required  :true, unique : true},
     contactName : {type: String, required  :true, unique : true},
     status : {type: String},
     email :  {type: String, lowercase : true, required  :true, unique : 
      true},
     phone : {type: Number, required  :true, unique : true},
     mobile : {type: Number, required  :true, unique : true},
})

module.exports = mongoose.model('ClientEmployee', ClientEmployeeSchema)

角度路线

更新公司路线

$scope.updateCompanies = function () {

$http.put('/api/companies',$scope.selectedComapny)
    .success(function(data) {

    });
};

添加clientemployee路线

$scope.addClientEmployee = function () { 
$http.post('/api/clientEmployees', $scope.clientEmployee)
    .success(function(response) {
        $scope.updateCompanies();
    });
 }

快速路线

更新公司路线

router.put('/companies', function(req, res){
Company
.findOne({companyName : req.body.companyName})
.populate('primaryContacts')
.exec()
});

客户员工路线

router.post('/clientEmployees', function(req, res) {
var clientEmployee = new ClientEmployee();
clientEmployee.username = req.body.username;
clientEmployee.contactName = req.body.contactName;
clientEmployee.status = req.body.status;
clientEmployee.email = req.body.email;
clientEmployee.phone = req.body.phone;
clientEmployee.mobile = req.body.mobile;

clientEmployee.save();

res.send('Employee created')
});`

1 个答案:

答案 0 :(得分:0)

首先,您必须创建一个ClientEmployee

newClientEmployee = new ClientEmployee({username: "Tetest", contact: "", ...});
newClientEmployee.save(function(err) {
    if (err) return;

    // Then you can attach him to the Company
    newCompany = new Company({companyId: "", companyName: "Corporate", ...});
    newCompany.primaryContacts.push(newClientEmployee._id);
    newCompany.save(function(err) {
        if (err) return;
    });
});

然后,为了让您的公司获得primaryContacts:

Company
.findOne({companyName : req.body.companyName})
.populate('primaryContacts')
.exec(function(err, company) {
    if (err) return;
    console.log(company); // Print your companies with your PrimaryContacts array
    console.log(company.primaryContacts[0]); // Will display "Tetest" if req.body.companyName = Corporate
})