1

I've read a few questions with this same issue but none work for my personal case. I'm trying to delete an entry off a ng-repeat list.

This is my view (the ng-shows are for editing purposes):

<tr ng-repeat="entry in entries" ng-cloak>
    <th>{{ $index + 1 }}</th>
    <td>
        <span ng-show="!editMode[$index]">{{ entry.username }}</span>
        <input ng-show="editMode[$index]" type="text" ng-model="entry.username"/>
    </td>
    <td>
        <span ng-show="!editMode[$index]">{{ entry.date }}</span>
        <input ng-show="editMode[$index]" value="{{entry.date}}"/>
    </td>
    <td>
        <span ng-show="!editMode[$index]">{{ entry.hours }}</span>
        <input ng-show="editMode[$index]" type="text" ng-model="entry.hours"/>
    </td>
    <td>
        <span ng-show="!editMode[$index]">{{ entry.payout }}</span>
        <input ng-show="editMode[$index]" type="text" ng-model="entry.payout"/>
    </td>
    <td>
        <button ng-show="!editMode[$index]" type="submit" class="btn btn-primary" ng-click="deleteEntry($index)">Delete</button>
        <button ng-show="!editMode[$index]" type="submit" class="btn btn-primary" ng-click="editEntry($index)">Edit</button>

        <button ng-show="editMode[$index]" type="submit" class="btn btn-primary" ng-click="saveUpdate($index)">Update</button>
        <button ng-show="editMode[$index]" type="submit" class="btn btn-primary" ng-click="cancelUpdate($index)">Cancel</button>

    </td>
</tr>

My angular controller:

// DELETE
$scope.deleteEntry = function(id) {
// delete entry from DB using clicked listing's id
$http.delete('/api/entry/' + id)
  .success(function(data) {
      $scope.entry = data;
  })
  .error(function(data) {
      console.log('Error: ' + data);
  });
// then update page with remaining entries
$http.get('/api/entries').then(function(response){
        $scope.entries = response.data;
    });
};

In my apiController.js

app.delete('/api/entry/:entry_id', function(req, res){           
  Entries.remove({
    _id : req.params.entry_id
  }, function(err, entry) 
    if (err)
      res.send(err);

    // get and return remaining entries
    Entries.find(function(err, entries) {
      if (err)
      res.send(err)
      res.json(entries);
    });
  });
});

When I remove the Entries.find function the error goes away but the delete call no longer works.

I'm using mongoose to connect to my DB.

What am I missing here?

PanicBus
  • 566
  • 1
  • 7
  • 17

3 Answers3

2

Two response return in same If block,so conflict headers.....

    app.delete('/api/entry/:entry_id', function(req, res){              

       Entries.remove({_id : req.params.entry_id},
      function(err, entry) 
        if (err){
          res.send(err);
         }     //end if block
        else{
        // get and return remaining entries
        Entries.find(function(err, entries) {
          if (err){
          res.send(err)
          } 
        else{
          res.json(entries);
           }
        });

    }  //end else block
 }); 

});
Zahidur Rahman
  • 1,688
  • 2
  • 20
  • 30
  • 1
    This actually solves the error, so I'm going to mark it as the correct answer, but unfortunately now my delete method does not work! It may be a different issue tho. – PanicBus Mar 29 '16 at 03:50
  • UPDATE: it was a different issue. Now working. THanks for the help! – PanicBus Mar 29 '16 at 04:03
1

put it in if else condition. after sending the response it continues and try to send again which causes this exception.

app.delete('/api/entry/:entry_id', function(req, res){           
  Entries.remove({
_id : req.params.entry_id
 }, function(err, entry) 
if (err)
  res.send(err);

else{
Entries.find(function(err, entries) {
  if (err)
  res.send(err) 
 else
  res.json(entries);
});
}
  });
});
Deendayal Garg
  • 5,030
  • 2
  • 19
  • 33
0

Your code sends an error response back to the client, then it tries to send a JSON response right afterward. The first response ends the request/response process, meaning the second response can no longer be sent and throws the can't set headers after they are sent error.

To solve this, I always return early in my error handling. This ends the request/response cycle, then returns from the function so that none of the code following it will be executed.

if (err) return res.send(err);
res.json(entries)
Community
  • 1
  • 1
Nocturno
  • 9,579
  • 5
  • 31
  • 39