sei-ec-remote / project-4-issues

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

Adding object id ref to created entry #97

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?

Trying to findbyid, then push an object id to the ref array

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

getOneDeveloper(dev)
              .then(developer => {
                console.log('DEVELOPER===================>>>\n', developer)
                developer.data.projects.push(res.data.project._id)
                developer.save()
              })
              .catch(err => {
                console.log(err)
                msgAlert({
                  heading: 'Error',
                  message: errorFindingDev,
                  variant: 'danger'
                })
              })

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

TypeError: Cannot read properties of undefined (reading 'push') at ProjectForm.js:119:1

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

Not sure - used this exact method before so expected this to work

What things have you already tried to solve the problem?

Checked documentation to be sure

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

aishcann commented 2 years ago

When you have an error like that, it means that whatever you are trying to push to is coming up as undefined. Run some console.logs to see why developer.data.projects is returning that way.

shaialoni commented 2 years ago

I was accessing it wrong - but now it's building the data correctly, but not saving it: image Is it because I am using a get route to get the file? Should I be using a patch route?

shaialoni commented 2 years ago

Got it, using a patch route instead. Thank you!

aishcann commented 2 years ago

Great!