2017-11-21 1 views
0

MongoDB에서 가져온 목록이 하나의 페이지에 표시되며 하나를 클릭하면 todo를 클릭 한 URL과 다른 페이지가 열립니다. 이제 URL에서 ID를 가져 와서 노드 서버로 보내려고하지만이를 가져올 수 없습니다.매개 변수를 가져올 수 없습니다 노드 및 각도

ngOnInit() { 
    this.route.params 
    .subscribe(
    (params: Params) => { 
     let todoId = params['userId']; 
     console.log(todoId); 
    } 
); 
} 

콘솔은 정의되지 않은 값을 반환합니다. ID가이 코드 줄에서 가져온 솔루션을 찾았지만 ID를 한 번만 가져오고 다른 할일을 클릭하면 아무 것도 기록하지 않습니다.

let id = this.route.snapshot.paramMap.get('id'); 
console.log(id) 

그리고 나는 이것으로 서버에 요청을 보낼 때

let id = this.route.snapshot.paramMap.get('id'); 
    this.todoService.getSingleTodo(id) 
    .subscribe(
     (todo: Todo) => { 
     this.todo = todo; 
     console.log(todo); 
     } 
    ); 

나는 "메시지"콘솔에서이 오류가있어 "ObjectId가로 캐스트는 값 \ 실패"아이디 \ "에서 경로 \ "_ 아이디 \"

서비스는 다음과 같습니다

getSingleTodo(id) { 
    return this.http.get('http://localhost:3000/todos/:id') 
     .map(response => response.json().obj) 
     .map(todo => todo.map(todo => new Todo(todo.todoHeadline, 
    todo.todoDescription, todo._id))); 
} 

그리고 노드 파일 :

router.get('/:id', (req, res, next) => { 
console.log(req.params.id); 
Todo.findById(req.params.id, (err, singleTodo) => { 
    if (err) { 
     return res.status(500).json({ 
      title: 'An error occured', 
      error:err 
     }); 
    } 
    res.status(201).json({ 
     message: 'Success', 
     obj: singleTodo 
     }); 
    }); 
}); 

또한이 콘솔은 다음을 인쇄합니다. id.

홈페이지 라우팅 파일 모두 왜 그렇게 문제가 될 수 무엇이

<ul class="list-group"> 
    <li class="list-group-item" 
     *ngFor="let todo of todos;" 
     [routerLink]="['/todos', todo.todoId]">{{todo.todoHeadline}} 
    </li> 
</ul> 

같은 lools을 표시 일하러

export const TODO_ROUTES: Routes = [ 
{path: 'todo/add', component: TodoAddComponent}, 
{path: ':id', component: TodoListComponent}, 
{path: 'edit', component: TodoEditComponent} 
]; 

HTML에 대한

const APP_ROUTES: Routes = [ 
{ path: '', redirectTo: '/auth', pathMatch: 'full' }, 
{ path: 'todos', component: TodoComponent, children: TODO_ROUTES}, 
{ path: 'auth', component: AuthenticationComponent, children: AUTH_ROUTES } 
]; 

그리고 어린이 노선?

답변

0

그것은 잘못 여기이 선 : 당신은에 액세스하려고 할 때 그래서

{path: ':id', component: TodoListComponent}, 

: 당신의 라우팅에서

let todoId = params['userId']; 

, 당신은 경로 변수 "ID"라는 것이라고 정의 path 변수 userId를 사용하면 분명히 undefined를 반환합니다.

+0

Omg, 어리석은 실수. 고맙습니다. 나는 실수를 찾으려고 지난 2 시간 동안 내 머리를 파열 시켰습니다. – Fanyasy

관련 문제