ga-wdi-boston / team-project

Other
2 stars 39 forks source link

Ajax - DELETE - returns 404 (filebucket) #352

Closed artpylon closed 7 years ago

artpylon commented 7 years ago

I am attempting to delete a file in my local db. I can see that the request includes the correct id of the file I'm trying to delete in the request URL, but it returns a 404.

My hunch is that this may be because the controller isn't looking for the file by the id...

events.js

const onDeleteFile = function (event) {
  event.preventDefault()
  const data = getFormFields(event.target)
  fileApi.deleteFile(data)
    .then(fileUi.deleteFileSuccess)
    .catch(fileUi.deleteFileFailure)
}

api.js

const deleteFile = function () {
  return $.ajax({
    // fileID to be stored locally when delete modal is launched
    url: config.apiOrigin + '/files/' + store.fileId,
    method: 'DELETE',
    headers: {
      Authorization: 'Token token=' + store.token
    }
  })
}

Controller destroy function:

const destroy = (req, res, next) => {
  req.file.remove()
    .then(() => res.sendStatus(204))
    .catch(next)
}
artpylon commented 7 years ago
screen shot 2017-06-28 at 11 41 45 am
artpylon commented 7 years ago

I added a console log inside the delete controller function, but it does not register in the server logs:

screen shot 2017-06-28 at 11 58 38 am
benjimelito commented 7 years ago

Would you mind posting your routes file? If that console.log is not firing, it seems like the issue could be there

benjimelito commented 7 years ago

Also, are you able to CURL it?

ghost commented 7 years ago

Routes: .resources('files', { only: ['index', 'show', 'destroy', 'create', 'update'] })

tvcrawley commented 7 years ago

Looks like your getting a 401 unauthorized. Are files created with an owners field?

ghost commented 7 years ago

The 401 disappears after signing according to the console.log above

benjimelito commented 7 years ago

What about the CURL request? Any success there, or same issue?

ghost commented 7 years ago

In progress will report back

ghost commented 7 years ago

Getting permission denied but no 401 error when CURL requesting DELETE

MicFin commented 7 years ago

What's the curl script look like?

ghost commented 7 years ago

ID=5953c9955de0ceb8156a6f96 TOKEN="X2ecjIjhOd/CLdb0tiYJ6F7UTwl5TzZI6m0k7j96KZY=--44sRR8buPoLagdTV91aj1Eb2QAajiqKSKWZb3nO+MAo=" assets/scripts/delete.sh

ghost commented 7 years ago
API="http://localhost:4741"
URL_PATH="/files"

curl "${API}${URL_PATH}/${ID}" \
  --include \
  --request DELETE \
  --header "Authorization: Token token=$TOKEN"

echo
ghost commented 7 years ago

DELETE issue fixed, issue was routes. Now looks like .resources('files', { only: ['index', 'show', 'create', 'destroy', 'update'] })

Update/PATCH is still at404 error though

MarcyNash commented 7 years ago

I still can't get the Update curl script or Update from the frontend. If DELETE is working, it's not the files schema at all and something is wrong with the update. I'm at a loss.

From the console logs in set-mongoose-model.js the File _id is not being correctly passed. I compared the examples schema to the files schema and noticed that at the bottom of the examples models file, module.exports = Example (Example is white) whereas the end of the files model file, module.exports = File (File is yellow).

DELETE should be going through the same mongoose-model code, yet still works.

payne-chris-r commented 7 years ago

Everything in this issue is about DELETE, not UPDATE. It's possible they both had the same thing wrong with them, but they also could be wildly different. Please post a new issue and close this one since the solution has been posted.

MarcyNash commented 7 years ago

Opening a new issue for UPDATE.

payne-chris-r commented 7 years ago

Can this be closed now? The solution is above. Please open and close the issue for UPDATE as well, @MarcyNash !

artpylon commented 7 years ago

delete works now, so closing this issue.