我有一个newGroups数组,它是一个组对象数组。这个数组包含一个id字段和实际的mongoDB objectID。项目包含一个名为“assignedGroups”的字段,它基本上也是一个组对象数组。我的函数应该获取用户选择的所有新组,并将其附加到所选项目的现有“assignedGroups”数组中。id是一个组的必填字段。但是,当我尝试将newGroups数组附加到“assignedGroups”数组时,ID字段不知何故丢失了,我不知道为什么会发生这种情况。
//add a group to a project given the client id and project id
router.post("/add_group", jwtVerify(['Admin', 'Manager']), expressAsyncHandler(
async (req, res) => {
console.log('req body: ', req.body);
const clientId = req.body.clientId;
const projectId = req.body.projectId;
const newGroups: group = req.body.newGroups;
try{
console.log("received client id: ", clientId);
const client = await ClientModel.findOne({ id: clientId });
if(client) {
const project = client.projects.find((project) => {
return project.id == projectId;
});
if(project) {
console.log("group is: ", newGroups);
project.assignedGroups?.push(...newGroups);
console.log('new project: ', project);
await client.save();
res.status(201).send(project);
} else {
res.status(404).send("Project not found");
}
} else {
res.status(404).send("Client not found")
}
} catch (error) {
console.log('error is: ', error);
res.status(500).send("Internal server error adding group to clients project");
}
}
));
字符串
我还收到以下错误:error is: Error: client validation failed: projects.0.assignedGroups.2.id: Path
idis required.
我已经尝试在所有地方打印控制台日志,并确定了导致错误的行。特别是当我尝试将newGroups推送到assignedGroups上时,它会发生,但我不知道为什么数据会发生变化。这是以下代码行的控制台日志的结果:
console.log("group is: ", newGroups);
project.assignedGroups?.push(...newGroups);
console.log('new project: ', project);
//OUTPUT
group is: [
{
_id: '6515880ccbb4d907d4aee87b',
id: '1',
groupName: 'Frontend',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1688755927/ayajgq9sgud81qzc.jpg',
people: [
'64b15aa72b77e4a923cafdc8',
'64b159e72b77e4a923cafda6',
'64b3c70fb49306cf1b0b6ff2',
'64b42de378a9f73779d70d66',
'64b432fd2512db8f87d31e75',
'64b43ea14a3e267132a5f292',
'6515867b44e85bf4fb3a6ddd',
'65159861578f8e1bf619aea1',
'65159c216ae2377b2ef80ff4',
'6515a4986ae2377b2ef8101e',
'64b159c52b77e4a923cafd9e',
'65161f64c8530d9744568efd',
'6536ebca578f8e1bf619d65d'
],
tickets: [
'1', '2', '3', '4', '5', '6', '7',
'8', '9', '10', '11', '12', '13', '15',
'16', '17', '18', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30',
'31', '32', '33', '34', '35', '36', '37',
'47', '53', '54', '55', '56', '57', '58',
'59', '60', '62', '65', '67', '68', '71'
],
__v: 1
}
]
new project: {
id: '8',
name: 'Org',
logo: '../../assets/project-logos/notebook.png',
color: 'pink-cl',
assignedGroups: [
{
id: '2',
groupName: 'Integration',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1695910934/ayajgq9sgud81qzc.jpg',
people: [Array],
tickets: [Array],
_id: new ObjectId("65158c173c0c30898fb9aa32"),
__v: 0
},
{
groupName: 'Security',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1695911006/ayajgq9sgud81qzc.jpg',
people: [Array],
tickets: [],
_id: new ObjectId("65158c5f3c0c30898fb9aa4f"),
__v: 0,
id: '4'
},
{
groupName: 'Frontend',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1688755927/ayajgq9sgud81qzc.jpg',
people: [Array],
tickets: [Array],
_id: new ObjectId("6515880ccbb4d907d4aee87b"),
__v: 1
}
],
_id: new ObjectId("6515a50078d33c3995f980b9"),
highPriorityTime: '1h',
lowPriorityTime: '1w',
mediumPriorityTime: '1d'
}
型
正如你所看到的,Frontend项目是一个应该被追加的项目,它确实不包含ID字段,但是,当打印newGroup时,它就在那里。任何帮助都将不胜感激。
//编辑我现在也尝试了一种稍微不同的方法,似乎没有进展:
if(client) {
const project = client.projects.find((project) => {
return project.id == projectId;
});
if(project) {
console.log("groups are: ", newGroups);
for (const newGroup of newGroups) {
const newGroupWithId = { ...newGroup, id: newGroup.id };
console.log("newGroupWithId is: ", newGroupWithId);
project.assignedGroups?.push(newGroupWithId);
if (project.assignedGroups && project.assignedGroups.length > 0) {
const lastGroup = project.assignedGroups[project.assignedGroups.length - 1];
console.log('last group: ', lastGroup);
if (!lastGroup.id) {
lastGroup.id = newGroupWithId.id;
console.log('last group in assignedGroups after id update: ', lastGroup);
}
}
}
console.log('new project: ', project);
await client.save();
res.status(201).send(project);
} else {
res.status(404).send("Project not found");
}
}
型
在这里,我尝试了一种非常手动的方法,首先确保在将组推送到assignedGroups时,组的id确实存在。我甚至检查assignedGroups中的最后一个组是否不包含id,然后添加id。代码进入最后的if块:if (!lastGroup.id)
,但仍然没有用我想要的id更新assignedGroups数组中的最后一个组!下面是输出:
newGroupWithId: {
_id: '6515880ccbb4d907d4aee87b',
id: '1',
groupName: 'Frontend',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1688755927/ayajgqes9sguidd81qzc.jpg',
people: [
'64b15aa72b77e4a923cafdc8',
'64b159e72b77e4a923cafda6',
'64b3c70fb49306cf1b0b6ff2',
'64b42de378a9f73779d70d66',
'64b432fd2512db8f87d31e75',
'64b43ea14a3e267132a5f292',
'6515867b44e85bf4fb3a6ddd',
'65159861578f8e1bf619aea1',
'65159c216ae2377b2ef80ff4',
'6515a4986ae2377b2ef8101e',
'64b159c52b77e4a923cafd9e',
'65161f64c8530d9744568efd',
'6536ebca578f8e1bf619d65d'
],
tickets: [
'1', '2', '3', '4', '5', '6', '7',
'8', '9', '10', '11', '12', '13', '15',
'16', '17', '18', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30',
'31', '32', '33', '34', '35', '36', '37',
'47', '53', '54', '55', '56', '57', '58',
'59', '60', '62', '65', '67', '68', '71'
],
__v: 1
}
last group: {
groupName: 'Frontend',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1688755927/ayajgqes9sguidd81qzc.jpg',
people: [
new ObjectId("64b15aa72b77e4a923cafdc8"),
new ObjectId("64b159e72b77e4a923cafda6"),
new ObjectId("64b3c70fb49306cf1b0b6ff2"),
new ObjectId("64b42de378a9f73779d70d66"),
new ObjectId("64b432fd2512db8f87d31e75"),
new ObjectId("64b43ea14a3e267132a5f292"),
new ObjectId("6515867b44e85bf4fb3a6ddd"),
new ObjectId("65159861578f8e1bf619aea1"),
new ObjectId("65159c216ae2377b2ef80ff4"),
new ObjectId("6515a4986ae2377b2ef8101e"),
new ObjectId("64b159c52b77e4a923cafd9e"),
new ObjectId("65161f64c8530d9744568efd"),
new ObjectId("6536ebca578f8e1bf619d65d")
],
tickets: [
'1', '2', '3', '4', '5', '6', '7',
'8', '9', '10', '11', '12', '13', '15',
'16', '17', '18', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30',
'31', '32', '33', '34', '35', '36', '37',
'47', '53', '54', '55', '56', '57', '58',
'59', '60', '62', '65', '67', '68', '71'
],
_id: new ObjectId("6515880ccbb4d907d4aee87b"),
__v: 1
}
last group in assignedGroups after id update: {
groupName: 'Frontend',
backgroundPhoto: 'https://res.cloudinary.com/ds2qotysb/image/upload/v1688755927/ayajgqes9sguidd81qzc.jpg',
people: [
new ObjectId("64b15aa72b77e4a923cafdc8"),
new ObjectId("64b159e72b77e4a923cafda6"),
new ObjectId("64b3c70fb49306cf1b0b6ff2"),
new ObjectId("64b42de378a9f73779d70d66"),
new ObjectId("64b432fd2512db8f87d31e75"),
new ObjectId("64b43ea14a3e267132a5f292"),
new ObjectId("6515867b44e85bf4fb3a6ddd"),
new ObjectId("65159861578f8e1bf619aea1"),
new ObjectId("65159c216ae2377b2ef80ff4"),
new ObjectId("6515a4986ae2377b2ef8101e"),
new ObjectId("64b159c52b77e4a923cafd9e"),
new ObjectId("65161f64c8530d9744568efd"),
new ObjectId("6536ebca578f8e1bf619d65d")
],
tickets: [
'1', '2', '3', '4', '5', '6', '7',
'8', '9', '10', '11', '12', '13', '15',
'16', '17', '18', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30',
'31', '32', '33', '34', '35', '36', '37',
'47', '53', '54', '55', '56', '57', '58',
'59', '60', '62', '65', '67', '68', '71'
],
_id: new ObjectId("6515880ccbb4d907d4aee87b"),
__v: 1
}
型
2条答案
按热度按时间yzuktlbb1#
要解决这个问题,您需要在将ID字段推送到assignedGroups数组之前将其复制到新嵌入的文档对象。您可以使用以下代码来完成此操作:
字符串
insrf1ej2#
更新组模型的架构:
字符串
将现有组数据迁移到新方案
型
将newGroups数组推送到assignedGroups数组:
型