WDI-SEA / project-2-issues

0 stars 0 forks source link

Need to sort array by distance which is not stored in db (calculated from lat/long) #60

Closed mgkdn9 closed 2 years ago

mgkdn9 commented 2 years ago

What is the general issue you're having: Right now I display all toolRequests in the order they appear in db. Instead I want to display them by the distance from that toolRequest's user to the currentUser Part of the problem is that I need to use currentUser in the calculation but they may not be signed in

This is where I think the issue is: // home route app.get('/', (req, res)=>{ db.user.findAll() .then((users) => { db.toolRequest.findAll({ include: [db.user] }) .then((toolRequests) => { console.log('This is a toolRequest:\n',toolRequests[1]) const sortedToolRequests = [toolRequests.length] const distance = [toolRequests.length] const deltaLat = 0, deltaLon = 0 toolRequests.forEach((tR,i) => { //calculate distance from currentUser to each toolRequest's user deltaLat = currentUser.latitude - tR.dataTypes.user.latitude deltaLon = currentUser.longitude - tR.dataTypes.user.longitude //distance = sum of squares of differences between coordinates distance[i] = Math.sqrt(Math.pow(deltaLat,2) + Math.pow(deltaLon,2)) }) console.log(distance) // res.render('home', {users, toolRequests}) changing to sortedToolRequests res.render('home', {users, toolRequests: sortedToolRequests}) }) }) })

Additional code: This is some of home.ejs where I render the sortedToolRequests array `<% toolRequests.forEach(function(tR) { %> <% if(!(currentUser.id===tR.user.id)) { %>

<%= tR.title %>

User:

<%= tR.user.name %>

` What I've tried so far (You must include three things you have tried): 1. tried doing calculation in index.js (as seen above) but it doesn't know what currentUser is yet 2. would try putting it in a different file and calling that file but no know why 3. am going to try doing the calculation in home.ejs but the ejs tags are getting crazy Error Message: currentUser is not defined
TaylorDarneille commented 2 years ago

do you have access to currentUser in the profile page ejs successfully?

TaylorDarneille commented 2 years ago

And can you post your route code in a screenshot or a code block so it shows with proper formatting? Hard too look at like this.

mgkdn9 commented 2 years ago

yes. though this will be shown on the home. yes I just noticed it formatted like that. my b

mgkdn9 commented 2 years ago

home.ejs:


  <h2>Showing all Tool Requests</h2>
  <% users.forEach((user) => { %>
    <div><%= user.name %></div>
  <% }) %>
<% } else { %>
<div style="background-color: whitesmoke;">
  <h3>Logged in as <%= currentUser.name %></h3>
  <h2>Showing Closest Tool Requests</h2>
  <% toolRequests.forEach(function(tR) { %>
    <% if(!(currentUser.id===tR.user.id)) { %>
      <article>
        <h3><%= tR.title %></h3>
        <div style="display: flex; gap: 15px; border: 1px solid black; border-radius: 5px; margin: 0; padding: 5px;">
          <div>
            <p>User:</p>
            <p><%= tR.user.name %></p>
          </div><div>
            <p>Created at:</p>
            <p><%= tR.createdAt %></p>
          </div>
          <div>
            <p>Need for:</p>
            <p><%= tR.timeNeeded %></p>
          </div>
          <div>
            <p>Offering:</p>
            <p><%= tR.priceFirstOffer %></p>
          </div>
          <div>
            <p>Located:</p>

            <p><%= tR.user.latitude %></p>
            <p><%= tR.user.longitude %></p>
          </div>
          <div>
            <p>Example Image:</p>
            <img src="<%= tR.pictureURL %>" alt="<%= tR.title %>" style="width: 200px;">
          </div>
        </div>
      </article>
    <% } %>
  <% }) %>
</div>
<% } %>
mgkdn9 commented 2 years ago

home route in index.js:


app.get('/', (req, res)=>{
    db.user.findAll()
    .then((users) => {
        db.toolRequest.findAll({
            include: [db.user]
        })
        .then((toolRequests) => {
            console.log('This is a toolRequest:\n',toolRequests[1])
            const sortedToolRequests = [toolRequests.length]
            const distance = [toolRequests.length]
            const deltaLat = 0, deltaLon = 0
            toolRequests.forEach((tR,i) => {
                //calculate distance from currentUser to each toolRequest's user
                deltaLat = currentUser.latitude - tR.dataTypes.user.latitude
                deltaLon = currentUser.longitude - tR.dataTypes.user.longitude
                //distance = sum of squares of differences between coordinates
                distance[i] = Math.sqrt(Math.pow(deltaLat,2) + Math.pow(deltaLon,2))
            })
            console.log(distance)
            // res.render('home', {users, toolRequests}) changing to sortedToolRequests
            res.render('home', {users, toolRequests: sortedToolRequests})
        })
    })
})
mgkdn9 commented 2 years ago

I think I can do the calculation inside home.ejs. It will just take lots of ejs tags.

TaylorDarneille commented 2 years ago

If you're getting the Error Message: currentUser is not defined still, it's because in your route, the user is called req.user (it's only currentUser in the ejs)

TaylorDarneille commented 2 years ago

I know that's not the issue you're describing but I got snagged on the error message

TaylorDarneille commented 2 years ago

actually that might solve the issue, now that I'm looking more deeply. Let me know!

TaylorDarneille commented 2 years ago

Is this resolved?

mgkdn9 commented 2 years ago

no still working on it

mgkdn9 commented 2 years ago

just need to figure out how to sort array of objects by one of the properties