【问题标题】:How should one handle different types of users in a MERN application?在 MERN 应用程序中应该如何处理不同类型的用户?
【发布时间】:2022-02-21 21:21:09
【问题描述】:

例如,我目前正在开发一个辅助项目来学习 MERN 堆栈。

应该如何处理不同类型的用户(在他们拥有不同领域的意义上)。例如,用户可能是客户(因此他可能拥有订单列表、购物车等内容),但另一个用户可能是供应商(供应商销售的产品列表、有关他的一般信息等)。

在诸如 Java 之类的语言中,这可能通过拥有一个 POJO 用户类然后拥有两个不同的实体(供应商和客户)利用继承机制来扩展具有基本属性的用户类(用户名、密码,电子邮件等)。

在 Mongoose 中使用 MongoDB 处理此问题的最佳方法是什么?

【问题讨论】:

    标签: node.js express mongoose mern


    【解决方案1】:

    您可以在 mongoose 用户模式中拥有角色,然后根据用户角色,您可以在前端显示不同的 ui,也可以在后端根据用户角色授权 api。 你可以试试这样的。

    const mongoose = require("mongoose");
    const userSchema = new mongoose.Schema({
      email: {
        type: String,
        required: [true, "please enter your email"],
        unique: true,
      },
      password: {
        type: String,
        required: [true, "please enter your passwword"],
        minlength: [6, "password should be minimum six charactors"],
        select: false,
      },
      role: {
        type: String,
        enum: ["Vendor", "Customer"],
        default: "Customer",
      },
    ///some other fields////
      });
       
    module.exports = mongoose.model("User", userSchema);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2011-11-03
      • 1970-01-01
      • 2010-10-07
      • 2021-10-18
      • 2016-04-20
      • 2022-07-04
      相关资源
      最近更新 更多