1

Am facing the casterror in my mongoose. I am sorry to ask this question again, but am unable to figure out my mistake. I am able to view the results through postman. But, I can't display the tasks on my frontend. When I am trying get the data from api to the frontend with TaskService am unable to fetch the data.

app.js

app.get('/lists/:listId/task',(req,res)=>{
    Task.find({
        _listId:req.params.listId
    }).then((task)=>{
        res.send(task);
    })
})


app.post('/lists/:listId/task',(req,res)=>{
    let newTask=new Task({
        title:req.body.title,
        _listId:req.params.listId
    });
    newTask.save().then((result)=>{
        res.send(result);
    })
})

app.patch('/lists/:listId/task/:taskId',(req,res)=>{
    Task.findOneAndUpdate({
        _listId:req.params.listId,
        _id:req.params.taskId
    },{$set:req.body})
    .then(()=>{
        res.sendStatus(200);
    })
});

app.delete('/lists/:listId/task/:taskId',(req,res)=>{
    Task.findOneAndRemove({
        _id:req.params.taskId,
        _listId:req.params.listId
    }).then((result)=>{
        res.send(result);
    })
})

config.ts

    export const baseURL=environment.production?'https://api.taskmanager.com':'http://localhost:3000';
    export const listURL=baseURL+'/lists';
    export const taskURL=baseURL+'/lists/:listId/task';

task.model.ts 

const taskSchema=new mongoose.Schema({
    title:{
        type:String,
        required:true

    },
    _listId:{
        type:mongoose.Types.ObjectId,
        required:true

    },
});

const Task=mongoose.model('Task',taskSchema);
module.exports=Task;

task.model.ts (In frontend)

export class Task{
    title:String;
    _listId:number;
    constructor(title:String,Id:number){
        this.title=title;
        this._listId=Id;
    }
}

task.service.ts

    export class TaskServiceService {
        
          constructor(private http:HttpClient) { }
        
     getTasks(id:string):Observable<Task[]>{
    let taskURL=`${baseURL}/lists/${id}/task`;
    return this.http.get<Task[]>(taskURL);
  }
      }

task-view.component.ts

ngOnInit(){
    this.getList();
    this.route.params.subscribe((params:Params)=>{
      console.log(params);
      this.taskService.getTasks(params.lisId).subscribe((tasks)=>{
        this.tasks=tasks;
      })
    });
  }

2 Answers2

0

In your config.ts, you set taskURL=baseURL+'/lists/:listId/task' so it will always send :listId as param listId to your server, that cause the cast error. To solve it, you can pass id to your service then create url from it. Something like:

getTasks(id: string):Observable<Task[]>{
  let taskURL = `${baseURL}/lists/${id}/task`;
  return this.http.get<Task[]>(taskURL);
}
Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39
0

This is because of the activated route method also getting called at the initial stage. But that the the _listId value will be undefined So you have to handle this

bala
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 19 '22 at 07:41