When working with a mongoose update query in typescript, I am trying to incorporate a template string. The specific field I need to update is a Map named messages, which consists of string keys and array values of type Message.
interface Message {
content: string,
from: string,
...
}
userSchema = new Schema({
...
messages: {type: Map, default: new Map<string, Message[]>}
}
The keys within the map represent ids of the "message-partner", such as userIds or groupIds. In the case of a new message being sent from a user with the id senderId to a user with the id receiverId, I intend to update both users' Maps. For instance, for the receiver, I would execute something similar to:
UserModel.findByIdAndUpdate(receiverId, { $push: { `messages.${senderId}`: newMessage } })
However, this approach results in some errors:
https://i.sstatic.net/dSJrw.png
If I modify the template string to a regular string instead, like so:
"messages.1234"
The errors disappear. I am curious if there are any workarounds for this issue, or if I am overlooking something in my implementation?