I need assistance with my Typescript code. I am currently working on a method that pushes data into an array in a mongoose collection. However, the issue I'm facing is that the value is not being passed dynamically to the Key field in the $set operator using the fieldName parameter. Can someone please guide me on how to modify this function so that it works as intended?
async createStudentInfo<T>(studentId:ObjectID, fieldName: String, fieldData:Array<T>, errorMessage: String):Promise<Array<T>>{
return new Promise(async (resolve, reject) => {
try {
const result = await studentModel.updateOne(
{ "_id": studentId },
{
$set: {
fieldName : fieldData
}
},
{upsert:true}
)
resolve(fieldData);
}
catch (err) {
reject(errorMessage);
}
});
}