Javascript Mongoose获取错误:MissingSchemaError:Schema haven';I don’我没有注册模特儿

Javascript Mongoose获取错误:MissingSchemaError:Schema haven';I don’我没有注册模特儿,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我遇到了这个错误,无法解决。我已将REF EXACLTY命名为模型: MissingSchemaError:尚未为模型“ParticipantStatus”注册架构。 以下是我的模型: 参与者状态型号: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const participantStatusSchema = new Schema({ _id: { type: Number, r

我遇到了这个错误,无法解决。我已将REF EXACLTY命名为模型: MissingSchemaError:尚未为模型“ParticipantStatus”注册架构。 以下是我的模型:

参与者状态型号:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const participantStatusSchema = new Schema({
  _id: {
    type: Number,
    required: true,
  },
  name: {
    type: String,
    required: true,
  },
});

module.exports = mongoose.model('ParticipantStatus', participantStatusSchema);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const eventParticipantSchema = new Schema(
  {
    userId: {
      type: Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    eventId: {
      type: Schema.Types.ObjectId,
      ref: 'Event',
      required: true,
    },
    teamId: {
      type: Schema.Types.ObjectId,
      ref: 'Team',
    },
    statusId: {
      type: Number,
      ref: 'ParticipantStatus',
      required: true,
    },
    isActive: {
      type: Boolean,
      required: true,
      default: true,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model('EventParticipant', eventParticipantSchema);
事件参与者模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const participantStatusSchema = new Schema({
  _id: {
    type: Number,
    required: true,
  },
  name: {
    type: String,
    required: true,
  },
});

module.exports = mongoose.model('ParticipantStatus', participantStatusSchema);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const eventParticipantSchema = new Schema(
  {
    userId: {
      type: Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    eventId: {
      type: Schema.Types.ObjectId,
      ref: 'Event',
      required: true,
    },
    teamId: {
      type: Schema.Types.ObjectId,
      ref: 'Team',
    },
    statusId: {
      type: Number,
      ref: 'ParticipantStatus',
      required: true,
    },
    isActive: {
      type: Boolean,
      required: true,
      default: true,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model('EventParticipant', eventParticipantSchema);
这是当我尝试获取事件参与者并填充statusId(ParticipantStatus)时引发错误的代码:


我在这件事上被困了好几个小时。只有.populate('statusId')部分抛出错误,其余部分工作正常。提前感谢

您要基于的字段类型应该是
ObjectId
,因此将
statusId
的类型从
Number
更改为
Schema.Types.ObjectId
,如下所示:

statusId: {
    type: Schema.Types.ObjectId,
    ref: 'ParticipantStatus',
    required: true,
  }

问题是你需要导入:

const ParticipantStatus = require('../models/participantStatus.model');
即使您没有在代码中直接引用它,在我看来这真的很奇怪。
希望这对任何人都有帮助。

参与者状态的id是一个数字而不是一个对象,我认为这不是问题所在。我在其他地方也做过类似的工作,比如在ObjectId上填充工作,您可以将teamId的类型更改为string,然后再次检查结果。如果没有,正如我提到的,我还有其他类似的情况