NodeJS Mongoose发现问题-类型错误:无法读取未定义的属性'find'

des4xlb0  于 2022-11-22  发布在  Node.js
关注(0)|答案(2)|浏览(156)

我在使用mongoose find()查询时遇到了问题,我无法解决这个问题。我收到的错误是“TypeError:无法读取undefined”“的属性'find',我怀疑这是导出/导入问题。如有任何帮助,将不胜感激。
下面是我方案模型文件:

const mongoose = require('mongoose');

const RoleSchema = new mongoose.Schema({
  pageGroup: { 
    type: String, 
    required: true,
  },
  level: { 
    type: String, 
    required: true,
  }
})

const OfficeSchema = new mongoose.Schema({
  officeId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: "Office",
    required: true,
  },
  roleId: { 
    type: [mongoose.Schema.Types.ObjectId], 
    required: false,
  },
})

const InstanceSchema = new mongoose.Schema({
  instanceId: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: "Instance",
    required: true,
  },
  offices: { 
    type: [OfficeSchema], 
    required: false,
  },
})

const UserSchema = new mongoose.Schema({
      firstName: { 
        type: String, 
        required: true 
      },
      lastName: { 
        type: String, 
        required: true 
      },
      email: { 
        type: String, 
        required: false 
      },
      password: { 
        type: String, 
        required: false 
      },
      access: {
        type: [InstanceSchema], 
        required: false,
      }, 
      permissions: {
        type: [RoleSchema], 
        required: false,
      },
      activationToken: {
        type: String,
        required: false,
      },
      roleId: { // new
          type: mongoose.Schema.Types.ObjectId,
          // index: true,
          ref: 'Role',
          // default: null
      },
      employeeId: {
          type: String, 
          required: false 
      },
      instanceId: {
          type: mongoose.Schema.Types.ObjectId, 
          ref: 'Instance', 
          required: true 
      },
      officeId: {
          type: mongoose.Schema.Types.ObjectId, 
          ref: 'Office', 
          required: true 
      }, 
    }, 
    {
      toJSON: { virtuals: true },
      toObject: { virtuals: true },
    })

UserSchema.virtual('instances', {
  ref: 'Instance',
  localField: 'access.instanceId',
  foreignField: '_id',
});

UserSchema.virtual('instances.offices', {
  ref: 'Office',
  localField: 'access.offices.officeId',
  foreignField: '_id',
});

UserSchema.virtual('office', {
  ref: 'Office',
  localField: 'officeId',
  foreignField: '_id',
  justOne: true,
});

UserSchema.virtual('name').get(function() {
  return this.firstName + " " + this.lastName
});

const User = mongoose.model('User', UserSchema);
module.exports = { User }

下面是我的控制器文件中的函数:

const { User } = require('./user.model');

async getEmployees(){
    
    const employees = await User.find({
            instanceId: this._id,
        }, '-password -activationToken -__v -activated')
        .populate('office')
        .sort([['firstName', 1]])
        .exec()

    return employees
},
eufgjt7s

eufgjt7s1#

该错误指出User未定义,当项目具有循环依赖项(其中文件A.js依赖于文件B.js,而B.js又直接或通过另一个文件间接依赖于文件A.js)时,可能会发生这种情况。
一个快速的解决方法是延迟加载User模型,直到真正需要它的时候,方法是将require()移到getEmployees()中:

async getEmployees(){
    const { User } = require('./user.model');
    
    const employees = await User.find({
            instanceId: this._id,
        }, '-password -activationToken -__v -activated')
        .populate('office')
        .sort([['firstName', 1]])
        .exec()

    return employees
}

但理想情况下,您应该完全消除循环依赖。

j9per5c4

j9per5c42#

我的项目也有同样的问题。您可以通过替换

const { User } = require('./user.model');

您的控制器:

const User = require('./user.model');

只要去掉括号就可以了。所以我想你应该用destructuring

相关问题