My Mongoose schema is structured as follows:
const gameSchema = new Schema({
matchNumber: {
type: Number,
required: [true, 'A match must have a number!'],
unique: true
},
red: {
s1: {
type: ObjectId,
ref: 'Match'
},
s2: {
type: ObjectId,
ref: 'Match'
},
s3: {
type: ObjectId,
ref: 'Match'
}
}
});
I am attempting to update the document with a Match through Express. When making a POST request to
:matchNumber/:alliance/:seed/:teamNumber/match
, I execute the following logic:
import * as flatten from 'flat';
let match = req.body;
const game = await Game.findOneAndUpdate(
{ matchNumber },
flatten({ [alliance]: { [seed]: match._id } }),
{ new: true }
);
However, upon sending the POST request, I receive the error message:
CastError: Cast to ObjectId failed for value "ObjectID" at path "red.s1"
It's worth noting that I am working with TypeScript and while this code was partially functional before, I came across some issues similar to those discussed here. The suggested solution to those problems seems to have caused the current error I am facing.