2017-09-22 5 views
0

이것은 분명히 알려진 문제이지만 내 MEAN 프로젝트에서이를 수정하는 방법을 모릅니다. 여러 요청이 전송 된 것으로 보입니다. 찾을 수있는 모든 솔루션을 시도했습니다 (이전 버전 또는 최신 버전). 지금까지 성공하지 못했습니다.Mongojs 보낸 후 헤더를 설정할 수 없습니다.

res.end() res.redirect는 ('/')

아무것도 트릭을 할 것 같다.

error in console

error in the terminal console

이 내 HTTP 방법

//delete 
router.delete('/task/:id', function(req, res, next) { 
    let post = mongojs.ObjectId(req.params.id) 
    db.tasks.remove({_id: mongojs.ObjectId(req.params.id)}, function(err, task){ 
     res.status(200).res.json(task) 
     return res.end() 

     console.log('SERVER - post ' + req.body.title + ' successfully deleted') 

     if (err) { 
      res.send('error message ' + err) 
     }  
    }) 
}) 

//update 
router.put('/task/:id', function(req, res, next) { 
    var task = req.body 
    var updateTask = {} 

    updateTask.isDone = task.isDone 
    updateTask.title = task.title 

    if(!updateTask) { 
     res.status(404).send({ 
      "error": "bad request" 
     }) 

    } else { 
     db.tasks.update({_id: mongojs.ObjectId(req.params.id)}, updateTask, {}, function(err, task){ 
      res.status(200).res.json(task) 
      return res.end() 

      if (err) { 
       res.send('error message ' + err) 
      } 
     }) 
    } 


}) 

이 각도 응용 프로그램입니다을 처리하는 곳, 그래서 여기에 내 코드 내 서비스 파일입니다. 하지만 클라이언트와 관련된 문제는 없습니다.

public deletePost(post: Post) { 
    console.log('SERVICE - post ' + post._id + ' successfully deleted') 
    return this.http.delete('http://localhost:1234/api/task/' + post._id) 
     .map(res => res.json) 

    } 

    public updatePost(postId : string, newText:string) { 

    } 

    public addPost(newPost: Post) { 
    var headers = new Headers();//?? 
    headers.append('Content-Type', 'application/json'); 
    console.log('successfully added (service)' + newPost.title) 
    let body = JSON.stringify({title: newPost.title, isDone:false}); 
    console.log(body) 


    return this.http.post('http://localhost:1234/api/task', JSON.parse(body),{headers}) 
    .map(res => res.json()); 
    } 
+0

가 해결! 클라이언트와 서버에서 실제로 두 번 요청을 보냈습니다. – SandyL

+0

이 (가) 대체되었습니다. res.status (200) .res.json (task) return res.end() – SandyL

답변

0

해결되었습니다. 실제로 클라이언트와 서버에서 두 번 요청을 보냈습니다. 고해상도 => res.json() 대체

: res.status (200) (클라이언트) 내 post.service.ts에서

, 나는 이미와 답을 전송했다. res.json (작업) 반환 res.end() 만에

: res.status (200)

관련 문제