The application contains various programs which have courses mapped to it. I'm trying to make modifications to the courses array using a drag and drop (dragging courses from 'Available Courses' to 'Mapped Courses' of a program).
'createProgram' function is called when an update request is made. It takes the initial values of the existingProgram, maps the new courses which come from a drap and drop component. Then passes the new object to the update route.
const createProgram = () => {
newProgram = {
_id: existingProgram._id,
info: {
name: existingProgram.name, // {required: true, unique: true, trim: true}
overview: existingProgram.overview, // {default: null, trim: true}
thumbnail: existingProgram.thumbnail, // {default: null, trim: true}
subtitle: existingProgram.name
.toLowerCase()
.replace(/ /g, '-')
.replace(/[^\w-]+/g, '')
.replace(/-+/g, '-'), // {required: true, unique: true, trim: true}
},
// populating 'courses' array with pre-existing values then mapping the new courses
// coming from 'Mapped Courses' column of react Drag & Drop
courses: [
...existingCourses,
columns['Mapped Courses'].items.map((course, index) => ({
id: course.id,
name: course.content,
sequence: index,
})),
],
};
axios
.put('https://updateProgramAPI/')
.then((response) => console.log(response))
.catch((err) => console.log(err));
};
But MongoDB is showing the following error:
code: 11000,
codeName: 'DuplicateKey',
keyValue: { 'courses.sequence': 1 },
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1637694265 },
signature: { hash: [Binary], keyId: [Long] }
}
The database isn't getting updated.