nodejs mongoose:检测到循环依赖

时间:2017-08-04 14:40:20

标签: node.js mongoose

我获得了循环依赖性异常,而此时我还没有在非常简单的代码中找到任何循环可能性。我预计它会因两个猫鼬模型之间的人口而发生,但如上所述:mongoose - possible circular dependency?这不应该产生这种错误,所以我不明白。

模型\ customer.js

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

var customerSchema = new Schema({
  _siretCustomer:
    { type: String, required: true, minlength: 15, maxlength: 15, index: {unique: true} },
  fileNumberCustomer:
    { type: String, required: true, minlength: 6, maxlength: 6 },
  companyTypeCustomer:
    { type: String, required: true },
  companyNameCustomer:
    { type: String, required: true },
  contactName1Customer:
    { type: String, required: false, default: null },
  contactName2Customer:
    { type: String, required: false, default: null },
  landlineContact1Customer:
    { type: String, required: false, default: null },
  landlineContact2Customer:
    { type: String, required: false, default: null },
  emailContactCustomer:
    { type: String, required: false, default: null },
  obligationCommentCustomer:
    { type: String, required: false, default: null },
  taskCommentCustomer:
    { type: String, required: false, default: null },
  creationDateCustomer:
    { type: Date, default: Date.now },
  userCustomer:
   [{ type: Schema.Types.ObjectId, ref: 'User', required: true }]
}, { collection: 'customer' });

module.exports = mongoose.model('Customer', customerSchema, 'customer');`

模型\ user.js的

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

var userSchema = new Schema({
    _trigramUser:
        { type: String, required: true, index: { unique: true } },
    lastnameUser:
        { type: String, required: true },
    firstnameUser:
        { type: String, required: true },
    privilegeUser:
        { type: String, required: true, enum: ['Accountant', 'Social operator'] },
    dateCreationUser:
        { type: Date, default: Date.now  },
    passwordUser:
        { type: String, required: true },
    customersUser:
        [{ type: String, ref: 'Customer'}]
}, { collection: 'user'});

module.exports = mongoose.model('User', userSchema, 'user');

控制器\会话\ customerController.js

exports.readCustomer = (req, res) => {
    database.getConnection();
    customer.find(req).populate('userCustomer').exec((err, customer) => {
    if (err) return res.status(500).json(err.stack);
    res.status(200).json(customer);
  });
};

0 个答案:

没有答案