sei-ec-remote / project-4-issues

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

Creating a model with many to many second model built in? #92

Closed shaialoni closed 2 years ago

shaialoni 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?

Create an entry with the object ID to another entry built in the object we use for the creation

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

function handleChange(e) {

    setProject(prevProject => {
        //let updatedValue = null
        let updatedValue = e.target.value
        updatedValue = updatedValue.charAt(0).toUpperCase()+updatedValue.slice(1)
        const updatedName = e.target.name

        //console.log('this is the input type', e.target.type)
        if (updatedName === 'developers') {

          getOneDevByName(updatedValue)
            .then(res => {
              console.log('RES.DATA from getOneDevByName', res.data.developer)
              dev = res.data.developer._id
              console.log('THIS IS updatedValue IN GETONEDEV======>>>\n', dev)
            })
            .catch(err => {
              console.log(err)
              msgAlert({
                heading: 'Error',
                message: errorFindingDev,
                variant: 'danger'
              })
            })

        }// else {
        //   updatedValue = e.target.value
        //   updatedValue = updatedValue.charAt(0).toUpperCase()+updatedValue.slice(1)
        // }

        const updatedProject = {
            [updatedName]: updatedValue
        }
        return {
            ...prevProject,
            ...updatedProject,
            developers: dev
        }
    })
  }
function handleSubmit(e) {
    e.preventDefault();
    // If this is acting out comment out from here====>>>>
    console.log('IN THE HANDLE SUBMIT BEFORE FORMDATA')
    const data = new FormData()
    data.append('upload', file)
    setLoading(true)
    createUrl(data)
      .then(res => {
        console.log('FIRST THEN IN CREATE URL=====================', project)
        myUrl.current = res.data.upload.url
        const image = myUrl.current
        console.log('THIS IS IMAGE===========>>>\n', image)

        const newProject = {
          ...project,
          img: image,
          developers: dev
        }

        console.log('ARE WE THERE YET', newProject)

        createProject(user, newProject)
          .then(res => {
            console.log('FIRST THEN IN CREATE PROJECT================', project, "RES FROM CREATE\N", res)
            navigate(`/projects/${res.data.project._id}`)})
          .then(() =>
            msgAlert({
              heading: "oh yea!",
              message: createProjectSuccess,
              variant: "success",
            })
          )
          .catch(() =>
            msgAlert({
              heading: "oh no!",
              message: createProjectFailure,
              variant: "danger",
            })
          );
      })
      .then(() => setLoading(true))
      .catch(err => {
        console.log(err)
        msgAlert({
          heading: "Error",
          message: errorUploadingImage,
          variant: "danger",
        })
      })
  }

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

Expecting to see the Project created with an object ID for developer in the developers field

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

Issue with the way we're using many to many

What things have you already tried to solve the problem?

Read up on setting up many to many relationships We know we can do it on a separate function, but are trying to create the project with the dev ref in it in one swoop.

Paste a link to your repository here https://github.com/tylly/project-4-mongoose-express

kestler01 commented 2 years ago

globally scoped variable was in state and not getting reassigned properly, added to state to manage assignment and route now works as intended.