R
R
Rag'n' Code Man2021-07-02 05:29:17
typescript
Rag'n' Code Man, 2021-07-02 05:29:17

Why does it give me an error, although there is no error?

TypeError: Mongoose maps only support string keys, got undefined


For some reason, I get such an error in any case, although there is no error anywhere, and the funny thing is that exactly the same code works in my Express project.

I have a student model, there are many fields, but we are only interested in one:
export default class Pupil extends TimeStamps {
    @prop({ type: () => Map, required: false, default: new Map() })
    localSchedule: Map<string, Schedule[]>;
}


There is a function that attaches the group schedule to the students of this group
private inheritGroupSchedule(
        pupils: DocumentType<Pupil>[],
        group: DocumentType<Group>,
        schedule: Schedule[]
    ) {
        pupils.forEach(async pupil => {
            pupil.localSchedule.set(group.id, schedule);

            await pupil.save();
        });
    }


And it is this block of code pupil.localSchedule.set(group.id, schedule);
that, for some reason, causes an error
TypeError: Mongoose maps only support string keys, got undefined


Even if we assume that , then maybe if I hardcode some string there, then everything will be fixed? - Not. Here is a screenshot of all this undebugged horror:group.id === undefined

60de79d8cf2cc412459821.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2021-07-02
@iDmitriyWinX

I have no idea why this works and the previous option doesn't, but it helped me to change the type from Map, to Schema.Types.Mixed
Ie .

export default class Pupil extends TimeStamps {
    @prop({ type: Schema.Types.Mixed, required: false, default: new Map() })
    localSchedule: Map<string, Schedule[]>;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question