I am facing a strange problem in my javascript code, I have function that returns a promise that contains all the tasks. Each task has a property project and this property contains an integer. Now, I want to fetch project by calling another function that also returns a promise so that I can convert the project property to a Project
object. But when I try to that I get this error
Unhandled Promise Rejection: TypeError: Attempted to assign to readonly property.
Here is my code
const allByProject = async (project: Project) : Promise<Task[]> => {
const allTasks = await TaskStore.allByProject(project);
allTasks.forEach(async (task: Task, i) => {
// In this case tasks return contains a project id instead of the project object
let projecId: any = task.project;
const project = await ProjectService.getById(projecId);
allTasks[i].project = project; // THIS GENERATES the ERROR.
});
console.log('All tasks: ', allTasks);
return allTasks;
}