【发布时间】:2020-06-30 16:06:47
【问题描述】:
我正在研究 MERN 应用程序,我对后端有疑问!我有 2 个模型用户和管理员,我希望将用户的数据发送给管理员,并且管理员可以管理任何用户(更新、删除、添加)
问题是我正在处理两条不同的路线(用户和管理员),我无法将用户的数据发送到管理员的数据
所以这是用户的架构
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const UserSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
birthdate:{
type: Date
},
age:{
type: Number,
pictureID: { type: mongoose.Schema.ObjectId, ref: "pictures"},
following: [{type: mongoose.Schema.ObjectId, ref: 'User'}],
followers: [{type: mongoose.Schema.ObjectId, ref: 'User'}]
});
module.exports = User = mongoose.model("users", UserSchema);
这是管理员的架构
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const AdminSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
birthdate:{
type: Date
},
age:{
type: Number,
},
pictureID: { type: mongoose.Schema.ObjectId, ref: "pictures"},
developers: [{type: mongoose.Schema.ObjectId, ref: 'User'}],
testers: [{type: mongoose.Schema.ObjectId, ref: 'User'}]
});
module.exports = Admin = mongoose.model("admins", AdminSchema);
【问题讨论】:
-
“你不能将用户的数据发送到管理员数据”是什么意思?
-
如果一个开发人员创建了一个我想要发送给管理员的新帐户,我的意思是它将被推送到管理员的“开发人员”数组中!因此管理员可以获取所有开发人员和测试人员并管理他们!