子模式属性不更新猫鼬
Posted
技术标签:
【中文标题】子模式属性不更新猫鼬【英文标题】:Sub-schema properties are not updated mongoose 【发布时间】:2022-01-12 07:07:32 【问题描述】:有点卡住了一个与猫鼬有关的问题。我有一个带有子模式(SingleUserSchema)的项目模式。每当我向此架构添加新用户时,它都会保存所有内容,这没关系。问题是,如果我更新用户,架构中的旧值不会更新。任何想法如何解决这种情况?已经在这里卡了一整天了。
架构:
const mongoose = require('mongoose');
const SingleUserSchema = new mongoose.Schema(
email:
type: String,
required: true,
,
role:
type: String,
required: true,
,
status:
type: String,
required: true,
,
);
const ProjectSchema = new mongoose.Schema(
name:
type: String,
required: [true, 'Provide project name'],
minlength: 5,
,
description:
type: String,
required: [true, 'Provide description about the project'],
,
maxWorkingEmployees:
type: Number,
required: [
true,
'Provide maximum number of employees working on this project',
],
,
currentlyWorkingEmployees: [SingleUserSchema],
status:
type: String,
enum: ['Pending', 'In progress', 'Paused', 'Delayed', 'Completed'],
default: 'Pending',
,
createdBy:
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
,
,
timestamps: true
);
module.exports = mongoose.model('Project', ProjectSchema);
控制器:
const attachEmployeeToProject = async (req, res) =>
const projectId, userId = req.params;
const project = await Project.findOne( _id: projectId );
const user = await User.findOne( _id: userId );
if (!user)
throw new NotFoundError(`User with id $userId does not exists`);
if (!project)
throw new NotFoundError(`Project with id $userId does not exists`);
const role, email, status = user;
const SingleUserSchema =
email,
role,
status,
;
let currentlyWorkingEmployees = [
...project.currentlyWorkingEmployees,
SingleUserSchema,
];
req.body.currentlyWorkingEmployees = currentlyWorkingEmployees;
const updateProject = await Project.findOneAndUpdate(
_id: projectId ,
req.body,
new: true,
runValidators: true,
);
res.status(StatusCodes.OK).json( updateProject );
;
也许我只需要创建一个参考?像这样尝试并收到大量错误,也迷失了如何在数组中创建 n 个引用。
currentlyWorkingEmployees: [
type: mongoose.Schema.ObjectId, ref: 'User', required: true ,
],
用户架构:
const UserSchema = new mongoose.Schema(
username:
type: String,
required: [true, 'Please provide username'],
minlength: 3,
maxlength: 50,
,
email:
type: String,
required: [true, 'Please provide username'],
unique: true,
validate:
validator: validator.isEmail,
message: 'Please provide valid email address',
,
,
password:
type: String,
required: [true, 'Please provide password'],
validator:
validate:
validator: validator.isStrongPassword,
message: 'Please provide stronger password',
,
,
,
firstname:
type: String,
required: [true, 'Please provide first name'],
,
lastname:
type: String,
required: [true, 'Please provide last name'],
,
status:
type: String,
enum: ['Working', 'Pause', 'Offline'],
default: 'Offline',
,
role:
type: String,
enum: [
'Developer',
'Analyst',
'Designer',
'Architect',
'Project Manager',
'Owner',
'Teamleader',
'Employee',
],
default: 'Employee',
,
verificationToken:
type: String,
,
isTokenVerified:
type: Boolean,
default: false,
,
tokenValidationDate:
type: Date,
default: null,
,
);
【问题讨论】:
【参考方案1】:首先,据我所知,您没有名为“用户”的模型,这就是您的参考不起作用的原因。
currentlyWorkingEmployees: [
type: mongoose.Schema.ObjectId, ref: 'User', required: true ,
]
https://mongoosejs.com/docs/populate.html
其次,如果我对您的问题的理解正确,您需要在 currentWorkingEmployees 集合中识别要更新的用户(基于 userId)。
希望对你有帮助!
【讨论】:
我没有在这里分享用户模型,但是我有。我的观点是,每当我创建一个用户并将其附加到项目时,然后当我更新用户时,我仍然会在项目中看到有关该用户的旧值。 我已经上传了用户架构以上是关于子模式属性不更新猫鼬的主要内容,如果未能解决你的问题,请参考以下文章