WDI-SEA / project-2-issues

0 stars 0 forks source link

More route issues: Cannot GET /apartments/search #52

Closed isaac8069 closed 2 years ago

isaac8069 commented 2 years ago

SEARCH FOR AN APARTMENT USING LOCATION

router.get('/search', (req, res) => {
  //   let {term} = req.query
  //Make lowerCase
  //   term = term.toLowerCase()
  console.log(req.query)
  db.apartment.findAll({
    where: { location: req.query.location }
  }).then((apartment) => {
    res.render('apartments/result', { apartment: apartment })
  }).catch((error) => {
    console.log(error)
    res.send('display apartments')
  })
})
isaac8069 commented 2 years ago
<%if(apartment===undefined) {%>
    <h3>No Results Found</h3>
    <small>check your spelling and punctuation and try again</small>
    <%} else {%>
        <ul>
            <%apartments.forEach(function(apartment) {%>
                <li>
                    <a href="/apartment/<%=apartment.title%>">
                        <%=apartment.title%>
                    </a>
                    <small>(<%=apartment.rent%>)</small>
                    <small>(<%=apartment.location%>)</small>
                </li>
                <%})%>
        </ul>
        <%}%>
isaac8069 commented 2 years ago
Screen Shot 2021-11-17 at 4 21 13 PM

I have the correct url! I don't know why its not hitting the correct route

isaac8069 commented 2 years ago

Three things I've tried:

  1. I tried changing the url around in my apartment.js controller
  2. I checked my result.ejs and index.ejs
  3. I reformatted the GET route for my search Error Message: Cannot GET /apartments/search
DoireannJane commented 2 years ago

I'm looking at your .render. Is your result.ejs within apartments in your directory structure?

isaac8069 commented 2 years ago

When you say directory structure what are you referring to?

DoireannJane commented 2 years ago

Your folders and files in your app.

DoireannJane commented 2 years ago

Your .render should be rendering an .ejs file in your views.

isaac8069 commented 2 years ago

Yes, result.ejs is within my apartments directory structure

Screen Shot 2021-11-18 at 10 18 38 AM

.

DoireannJane commented 2 years ago

In your controllers middleware, are you requiring the apartments url path?

isaac8069 commented 2 years ago
Screen Shot 2021-11-18 at 10 27 03 AM

Yes, I am requiring the apartments url path

isaac8069 commented 2 years ago

The console log is bringing up another routes console log. When I comment the route out it brings up the search console log message. Not sure why. They have different URLs

DoireannJane commented 2 years ago

I was looking for the middleware. App.use

DoireannJane commented 2 years ago

Your GET route looks fine.

DoireannJane commented 2 years ago

Could you show me the middleware? Should look like this: TAYLOR'S SNIPPET BELOW

isaac8069 commented 2 years ago

I do not have that in my middleware.

TaylorDarneille commented 2 years ago

Actually the middleware should say app.use('/apartments', require('./controllers/apartments')). If your current middleware is showing a singular /apartment, then it means that the action on your search form is wrong and should be /apartment (currently it's showing /apartments)

isaac8069 commented 2 years ago
Screen Shot 2021-11-18 at 1 46 01 PM

Thats what I have

isaac8069 commented 2 years ago
Screen Shot 2021-11-18 at 1 47 01 PM

This is my form! It matches my controller in index.js

TaylorDarneille commented 2 years ago

resolved!