Reputation: 9
Giving error Property 'element' does not exist on type 'ObjectId'.Maybe Related to Schema. Model and codes are written in Typescirpt.
Map Model:
interface IMap extends Document {
width: number;
height: number;
name: string;
thumbnail?: string;
mapElements: mongoose.Schema.Types.ObjectId[];
}
const MapSchema: Schema<IMap> = new Schema({
width: { type: Number, required: true },
height: { type: Number, required: true },
name: { type: String, required: true },
thumbnail: { type: String },
mapElements: [{ type: mongoose.Schema.Types.ObjectId, ref: 'MapElements' }]
});
MapElement Model:
interface IMapElements extends Document {
map: mongoose.Schema.Types.ObjectId;
element: mongoose.Schema.Types.ObjectId;
x?: number | null;
y?: number | null;
}
const MapElementsSchema: Schema<IMapElements> = new Schema({
map: { type: mongoose.Schema.Types.ObjectId, ref: 'Map', required: true },
element: { type: mongoose.Schema.Types.ObjectId, ref: 'Element', required: true },
x: { type: Number, default: null },
y: { type: Number, default: null }
});
Error Code: Here is Getting Property 'element' does not exist on type 'ObjectId' Error in e.element and e.x and e.y also showing similar error
const map = await Map.findOne(
{ _id: parsedData.data.mapId },
{ width: 1, height: 1, mapElements: 1 }
).populate({
path: 'mapElements',
select: 'element x y',
});
console.log(map);
if (!map) {
res.status(400).json({ message: "Map not found" });
return;
}
const spaceElementsData = map.mapElements.map((e) => ({
space: space._id,
element: e.element._id,
x: e.x!,
y: e.y!
}));
Upvotes: 0
Views: 24