【问题标题】:How to implement mongoose discriminators?如何实现猫鼬鉴别器?
【发布时间】:2022-01-26 20:22:56
【问题描述】:

我想在我的项目中使用猫鼬鉴别器来创建一个用户集合,其中有一个我想使用鉴别器实现的所有者文档。但我收到一个错误

throw new Error('mongoose.model() 的第二个参数应该是 ' + ^

错误:mongoose.model() 的第二个参数应该是模式或 POJO 在 Mongoose.model (D:\Github\Food-Delivery-Website\node_modules\mongoose\lib\index.js:473:11) 在对象。 (D:\Github\Food-Delivery-Website\models\Owner.js:21:27)

代码如下:

// This file is models/User.js
const mongoose = require('mongoose');
const { Schema } = mongoose;

const options = { discriminatorKey: 'kind' };

const UserSchema = new Schema(
  {
    userName: {
      type: String,
      required: true,
      unique: true,
    },
    restOwned: {
      // type: [Schema.Types.ObjectId],
      type: Number,
    },
  },
  options,
);

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

下面是下一个文件

// This file is models/Owner.js
const mongoose = require('mongoose');
const { Schema } = mongoose;

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

const OwnerSchema = User.discriminator(
  'Owner',
  new Schema({
    isOwner: {
      type: Boolean,
      required: true,
    },
    restName: {
      type: String,
      required: true,
    },
  }),
);

module.exports = mongoose.model('Owner', OwnerSchema);

然后我在userController.js中导入这两个文件

//This file is controllers/userController.js

const User = require('../models/User');
const Owner = require('../models/Owner');

exports.addUser = async (req, res) => {
  try {
    const newUser = new User({
      userName: req.body.userName,
      restOwned: req.body.restOwned,
    });

    const user = await newUser.save();

    res.status(201).json({
      status: 'Success',
      user,
    });
  } catch (err) {
    res.status(500).json({
      status: 'failed',
      message: 'Server Error: Failed Storing the Data.',
      err,
    });
  }
};

exports.addOwner = async (req, res) => {
  try {
    const newOwner = new Owner({
      isOwner: req.body.isOwner,
      restName: req.body.restName,
    });

    const owner = await newOwner.save();

    res.status(201).json({
      status: 'Success',
      owner,
    });
  } catch (err) {
    res.status(500).json({
      status: 'failed',
      message: 'Server Error: Failed Storing the Data.',
      err,
    });
  }
};

我在这里做错了什么?

enter image description here

【问题讨论】:

    标签: javascript node.js mongodb mongoose discriminator


    【解决方案1】:

    Model.discriminator() 方法返回一个模型。

    所以可以直接导出判别器,作为模型使用

    // This file is models/Owner.js
    const mongoose = require('mongoose');
    const { Schema } = mongoose;
    
    const User = require('./User');
    
    //Directly export the discriminator and use it as the model
    module.exports = User.discriminator(
      'Owner',
      new Schema({
        isOwner: {
          type: Boolean,
          required: true,
        },
        restName: {
          type: String,
          required: true,
        },
      }),
    );
    
    //module.exports = mongoose.model('Owner', OwnerSchema);
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 2017-09-24
      • 1970-01-01
      • 2020-05-22
      • 2018-07-19
      • 2022-06-30
      • 2021-03-25
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多