【问题标题】:Importing a schema into another file将架构导入另一个文件
【发布时间】:2021-07-24 17:16:41
【问题描述】:

我正在尝试将 Punishment 架构导入我的 guild.js,但我不知道如何导入它。

如你所见,我有惩罚.js,但我需要将惩罚模式包含到 guild.js 中,因为它依赖于它

Punishments.js

const mongoose = require("mongoose");

const punishmentType = Object.freeze({
  WARN: "Warn",
  BAN: "Ban",
  MUTE: "Mute",
});

const punishmentSchema = mongoose.Schema({
  userId: String,
  type: {
    type: String,
    enum: Object.values(punishmentType),
  },
});

exports.punishmentSchema = punishmentSchema;
exports.Punishment = mongoose.model("Punishment", punishmentSchema);

Guild.js

const mongoose = require("mongoose");

const guild = mongoose.Schema({
  id: String,
  prefix: String,
  punishments: [Punishment],
});

const Guild = mongoose.model("Guild", guild);
module.exports = Guild;

【问题讨论】:

    标签: node.js mongodb mongoose discord.js


    【解决方案1】:

    只是一个普通的require

    const {Punishment} = require('./Punishments');
    

    【讨论】:

    • 我这样做了,但我收到了一个错误,指出架构配置无效:model 不是数组punishments 中的有效类型。有关有效架构类型的列表,请参阅 bit.ly/mongoose-schematypes。
    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 2012-02-01
    • 2012-10-08
    • 2021-06-22
    • 2012-12-15
    • 2021-03-21
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多