sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

undefined parameter in axios call in api folder api/projects #69

Closed clds84 closed 2 years ago

clds84 commented 2 years ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

MERN

What's the problem you're trying to solve?

I'm trying to delete a document in my show route and when i click delete, I get this:

xhr.js:210 DELETE http://localhost:8000/projects/undefined 422 (Unprocessable Entity)

Post any code you think might be relevant (one fenced block per file)

export const removeProject = (user,projectId) => {
    console.log('user', user)
    console.log('this is projectId', projectId)
    return axios({
        url: `${apiUrl}/projects/${projectId}`,
        method: 'DELETE',
        header: {
            Authorization:`Token token=${user.token}`
        },
    })
}

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

What is your best guess as to the source of the problem?

I tried console logging console.log('this is projectId', projectId) and it came up as undefined which explains the error above that says /projects/undefined, but I'm not sure why. I looked at the pets example and a similar thing was done, petId as a parameter in the removePet function in the api folder in the client side. Not sure where I might be going wrong.

What things have you already tried to solve the problem?

I've tried changing the delete route to include the req.body like I did for create and update:

 data: { project: {
            owner: user._id,
            type: updatedProject.type,
            pattern: updatedProject.pattern,
            fabric: updatedProject.fabric,
            interfacing: updatedProject.interfacing,
            notions: updatedProject.notions,
        }}

but it doesn't make sense to me to have to do this and well, it doesn't work : ) Paste a link to your repository here

https://github.com/clds84/Project-4-Client https://github.com/clds84/Project-4-API

cory-yonomi commented 2 years ago

Can you add the delete route as well?

cory-yonomi commented 2 years ago

Also add the codeblock where removeProject() is getting invoked

clds84 commented 2 years ago

I managed to get it to work after commenting out requireOwnership. I think this is okay bc in the create function, as per what I mentioned above, I wrote it out the way it looks in the NodeJs - Axios bit in Postman, but also included owner: user._id. Christos helped me with this with my create function and his logic was that since I have that there, it's okay not to include requireOwnership.

Delete route in backend:

router.delete('/projects/:id', (req, res, next) => {
    Project.findById(req.params.id)
        .then(handle404)
        .then((project) => {
            // throw an error if current user doesn't own `project`
        //requireOwnership(req, project)
            // delete the project ONLY IF the above didn't throw
            project.deleteOne()
        })
        // send back 204 and no content if the deletion succeeded
        .then(() => res.sendStatus(204))
        // if an error occurs, pass it to the handler
        .catch(next)
})

removeProject invoked:

    const RemoveProject = () => {
        removeProject(user, id)

            .then(() =>
                msgAlert({
                    heading: 'Project removed',
                    message: 'there will always be another',
                    variant: 'success',
            }))
            .then(() => {navigate(`/projects`)})
            .catch(() =>
                msgAlert({
                    heading: 'Dang!',
                    message: 'no workie',
                    variant: 'danger',
            }))
    }