嗨,我刚开始开发一个平均堆栈项目,我的任务是我有两个集合--一个是管理的用户,另一个是疫苗中心的用户--我的任务是将一个管理员注册到一个医疗中心,但我不确定如何链接这些集合,因为管理员需要查看注册的医疗中心,然后将它们注册到中心。
user.js (猫鼬模式),
const userSchema = mongoose.Schema({
username: {type: String, required: true, unique: true},
password: {type: String, required: true},
fullname: {type: String, required: true},
email: {type: String, required: true},
staffid: {type: String, required: true, unique: true},
center: {type: String, required: true},
});
userSchema.plugin(uniqueValidator);
module.exports = mongoose.model('User', userSchema);,,,
center.js
,,,
const mongoose = require('mongoose');
const postSchema = mongoose.Schema({
cname: {type: String, required: true},
caddr: {type: String, required: true}
});
module.exports = mongoose.model('Centers', postSchema);,,,
user.model.service
,,,
export interface User{
id: string;
fname: string;
uname: string;
email: string;
pass: string;
staffid: string;
center: string;
},,,
. name (我计划将中心名称列表显示为下拉列表,以便管理员在注册帐户时选择中心名称),
<mat-form-field appearance="fill">
<mat-label>Select</mat-label>
<mat-select >
<mat-option name="center" *ngFor="let post of posts" value="post">{{post.cname}}</mat-option>
</mat-select>,,, app.js,,,
app.post('/api/user/signup', (req, res, next) => {
bcrypt.hash(req.body.password, 10)
.then(hash => {
const user = new User ({
username: req.body.username,
password: hash,
fullname: req.body.fullname,
email: req.body.email,
staffid: req.body.staffid,
center: req.body.center
});
user.save()
.then(result => {
res.status(201).json({
message: 'user registered',
result: result
});
})
.catch(err =>{
res.status(500).json({
error:err
});
});
});
});,,,
发布于 2022-01-22 12:19:19
根据您的描述,我认为您需要三个数据库表或三个mongoose.Schema:
一种用于疫苗接种中心:类似于用于(管理)用户的center.js
user.js,但不包括center property
mongoose.Schema({
user: {type: schema.type.objID, ref: "Users", required: true},
center: {type: schema.type.objID, ref: "Centers", required: true}
});如果一个用户可以被分配到多个疫苗接种中心,则需要额外的关联模式。
这对你的要求有帮助吗?
https://stackoverflow.com/questions/70812475
复制相似问题