在后端我有两个模式:
用户:
var UserSchema = new mongoose.Schema({
isActive: {type: Boolean, default: false},
isVerified: {type: Boolean, default: false},
firstName: {type: String, required: [true, "can't be blank"]},
lastName: {type: String, required: [true, "can't be blank"]},
email: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true},
phone: {type: Number, unique: true, required: true, required: [true, "can't be blank"]}, // validate phone
role: {type: String, enum: ['invalid', 'precustomer', 'customer', 'broker', 'brokerAdmin', 'dealer']},
hash: String,
salt: String,
address: {type: Object, required: [isRequired('address'), "can't be blank"]}, //require customer
organization: { type: mongoose.Schema.Types.ObjectId,
ref: 'Organization',
required: [isRequired('organization'), "can't be blank"]}, //require broker && dealer
deliverySchedule: String // for now.. make this custom obj soon --- OPTIONAL
}, {timestamps: true});
应用:
var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
var ApplicationSchema = new mongoose.Schema({
customer: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: [true, "can't be blank"]},
dealer: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: [true, "can't be blank"]},
broker: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: [true, "can't be blank"]},
files: {type: mongoose.Schema.Types.ObjectId, ref: 'Files', required: [true, "can't be blank"]},
}, {timestamps: true});
mongoose.model('Application', ApplicationSchema);
};
在前端,为了经纪人"创建一个"应用程序,"他必须首先添加一个"客户"到申请。为此,Broker将通过电子邮件搜索用户,服务器可以使用以下任一方式进行响应:
电子邮件存在(用户对象= id,电子邮件,...)
找不到用户({})
无法将_id存储在"创建应用程序" API端点,因为当一个" Broker"创建一个"应用程序","客户"在他们的电子邮件中发送一个链接注册(新_id),并且链接到应用程序的_id丢失(据我所知)。我希望我的"客户" "经销商"和"经纪人"是String
而不是mongoose.Schema.Types.ObjectId
,但我希望它能与.populate('客户经销商经纪人')一起使用电子邮件而不是ID。
从用户架构中删除名称,电话等所需的验证更有意义,只需要一封电子邮件即可注册"注册"并要求"客户"点击确认链接后?