簡體   English   中英

貓鼬關系一對多不起作用

[英]Mongoose relation one-to-many not work

我的貓鼬模式關系一對多存在問題:我有兩個文檔company和agent,當我嘗試保存公司文檔時無法保存。 注意:父文檔是公司,子文檔是代理商:

公司架構

'use strict';

/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

/**
* Company Schema
*/
var CompanySchema = new Schema({
name: {
    type: String,
    default: '',
    //required: 'Please fill Company name',
    trim: true
},
created: {
    type: Date,
    default: Date.now
},
updated: {
    type: Date,
    default: Date.now
},
address: {
    type: String,
    default: '',
    //required: 'Please fill Company address',
    trim: true
},
locked: {
    type: Boolean,
    default: true,
},
deleted: {
    type: Boolean,
    default: false,
},
logo: {
    type: String,
    default: '',
},
email: {
    type: String,
    default: '',
    //required: 'Please fill Company email',
},
tel: {
    type: String,
    default: '',
    //required: 'Please fill Company tel',
},
fax: {
    type: String,
    default: '',
    //required: 'Please fill Company fax',
},
type: {
    type: String,
    //required: 'Please fill Company type',
    trim: true
},
description: {
    type: String,
    default: '',
    trim: true
},
user: {
       type: Schema.ObjectId,
       ref: 'User'
   },
   agents: [{
      type: Schema.ObjectId,
      ref: 'Agent'
   }]
});

mongoose.model('Company', CompanySchema);

代理架構

'use strict';

/**
* Module dependencies.
*/
 var mongoose = require('mongoose'),
Schema = mongoose.Schema;

/**
* Agent Schema
*/
var AgentSchema = new Schema({
// Agent model fields   
// ...
firstname: {
    type: String,
    default: ''
},
lastname: {
    type: String,
    default: ''
},
email: {
    type: String,
    default: ''
},
password: {
    type: String,
    default: ''
},
roles: {
    type: Array,
},
created: {
    type: Date,
    default: Date.now
},
updated: {
    type: Date,
    default: Date.now
},
deleted: {
    type: Boolean,
    default: false
},
locked: {
    type: Boolean,
    default: false
},
company: {
    type: Schema.ObjectId,
    ref: 'Company'
  }
});

var exports = module.exports = mongoose.model('Agent', AgentSchema);

API

company.save(function(err){
  if(err){
    console.log('company err');
    return res.status(400).send({
      messages: errorHandler.getErrorMessage(err)
    });
  }else{
    console.log('company save');
    var agents = preapareAgents(req, company);
    Agent.create(agents, function(err){
      if(err){
        return res.status(400).send({
          messages: errorHandler.getErrorMessage(err)
        });
      }else{
        res.jsonp({
          company: company,
          agents: agents
        });
      }
    });
  }
});

prepareAgent

var preapareAgents = function(req,company){
var admin = new Agent(req.body.admin);
admin.roles = ['admin', 'read','write'];
admin.company = company;
admin.locked = false;

var agents = req.body.agents;

for (var i = 0; i <= agents.length - 1; i++) {
    agents[i].roles = [agents[i].role];
    agents[i] = new Agent(agents[i]);
    agents[i].company = company;
}

agents.push(admin);

return agents;
};

****錯誤:
CastError:在路徑“代理”中,對值“ [object Object]”的轉換為ObjectId失敗

在prepareAgents方法中,每個代理的公司必須是公司的_id,而不是對象公司。 您的代碼應如下所示:

for (var i = 0; i <= agents.length - 1; i++) {
    agents[i].roles = [agents[i].role];
    agents[i] = new Agent(agents[i]);
    agents[i].company = company._id; //here is the problem
}

試試吧,也許我錯了。

這不是問題的根源,但是當我從有效的公司模型中刪除代理集合時,此行會導致問題:

   agents: [{
     type: Schema.ObjectId,
     ref: 'Agent'
   }]

我通過使用其他名稱重命名代理集合來解決問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM