2017-09-22 131 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

解決! 我確實在客戶端和服務器端發送了兩次請求。

在我post.service.ts(客戶端),我已經發送答案用: RES => res.json()

代替: res.status(200)。 res.json(任務) 回報res.end()

只有: res.status(200)

相關問題