WDI-SEA / project-4-issues

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

Having trouble accessing a document within an array #57

Closed LindZCoding closed 2 years ago

LindZCoding 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 go to the next chapter based off of the title

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


    // grab story where title is ''
    console.log('title', story)
    Dialogue.dialogueModel.findOne({
        story: {
            title: req.params.title
        }
    })```

### 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?
It is not finding a chapter where the title is ""

### What things have you already tried to solve the problem?

```router.get('/dialogues/:title', requireToken, (req, res, next) => {
    // grab story where title is ''
    console.log('title', req.params.title)
    Dialogue.dialogueModel.findOne({
        title: req.params.title
    })
        // .then(handle404)
        // if `findById` is succesful, respond with 200 and "example" JSON
        .then(dialogue => {
            console.log('dialogue found here: ', dialogue)
            res.status(200).json({ dialogue: dialogue.toObject() })
        })
        // if an error occurs, pass it to the handler
        .catch(next)
})```
LindZCoding commented 2 years ago

apiStory

example of what the model info looks like

timmshinbone commented 2 years ago

what does the console log show? the one that says console.log('title', req.params.title)

LindZCoding commented 2 years ago

it shows whatever i put in at the end of that: title 1

LindZCoding commented 2 years ago

when i try to find it this way:


        story: {
            title: req.params.title
        }
    })```
it gives me back: dialogue found here:  null

 11:22:51 GMT-0800 (Pacific Standard Time):
TypeError: Cannot read properties of null (reading 'toObject')

vs when i had it like 

``` Dialogue.dialogueModel.findOne({
        title: req.params.title
    })```

it gave me no error but it only will send back info on title 1 and nothing else
timmshinbone commented 2 years ago

hmmmmm, let's look in a breakout room