**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2小时前关闭
Improve this question
我有功能从子组中删除用户,并为每个用户使用此功能。
import { Branch } from "../../../models/index.js";
export default async function removeASubGroupUser(
branchId,
groupKey,
subGroupId,
userData
) {
const branch = await Branch.findById(branchId);
const group = branch.groups.find((group) => group.name === groupKey);
const subGroup = group.subGroups.find(
(subGroup) => subGroup._id.toString() === subGroupId
);
const userToRemove = subGroup.users.findIndex(
(user) => user._id.toString() === userData._id.toString()
);
console.log(subGroup.users);
subGroup.users.splice(userToRemove, 1);
branch.save();
console.log(subGroup.users);
const branchDataIndex = userData.branchesData.findIndex(
(branchData) =>
branchData.branchId.toString() === branch._id.toString() &&
branchData.groupKey === groupKey &&
branchData.subGroupId.toString() === subGroupId
);
userData.branchesData.splice(branchDataIndex, 1);
// await updatedUser.save();
return userData;
}
之后在主文件中使用了这个函数,但我不知道是什么问题
const usersData = await Promise.all(
validated.map(async (user) => {
const validatedInput = await validateStudentExcelInput(
user,
branchModel
);
let registeredUser = await User.findOne({
nationalId: validatedInput.nationalId,
});
if (registeredUser) {
if (registeredUser.branchesData[0].branchId) {
registeredUser = await removeASubGroupUser(
registeredUser.branchesData[0].branchId,
registeredUser.branchesData[0].groupKey,
registeredUser.branchesData[0].subGroupId,
registeredUser
);
}
这是 Mongoose 模式
import mongoose from "mongoose";
import Address from "../shared/Address.js";
import Calendar from "../shared/Calendar.js";
import Group from "./Group.js";
import rejectedExcel from "./rejectedExcel.js";
import validatedExcel from "./validatedExcel.js";
const { Schema } = mongoose;
const branchSchema = new Schema(
{
providerId: {
type: Schema.Types.ObjectId,
ref: "Provider",
},
aId: {
type: String,
},
name: {
type: String,
},
branchType: {
type: String,
},
email: {
type: String,
},
phoneNumber: {
type: String,
},
address: Address.schema,
workingHours: Calendar.schema,
// employees, not patients
branchUsers: [
{
userId: {
type: Schema.Types.ObjectId,
ref: "User",
},
// ADMIN can CRUD all operations in the branch
role: {
type: String,
},
position: {
type: String,
},
category: {
type: String,
},
speciality: {
type: String,
},
// calendars: [Calendar.schema],
},
],
excelFileURL: {
type: String,
},
groups: [Group.schema],
validatedExcel: [validatedExcel.schema],
rejectedExcel: [rejectedExcel.schema],
schemaVersion: {
type: String,
default: "1.0.0",
},
},
{ versionKey: false, timestamps: true } // this adds createdAt and updatedAt fields to the schema
);
export default mongoose.model("Branch", branchSchema);
1条答案
按热度按时间pxyaymoc1#
你需要像下面这样在
.save()
方法中传递分支的findById()
响应。改变你的.save()
方法,它会帮助你。但它不会更新,它将创建新文档。要更新文档,您必须使用
findByIdAndUpdate()
有关findByIdAndUpdate()
的更多说明,请访问此链接https://www.geeksforgeeks.org/mongoose-findbyidandupdate-function/