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
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)
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 %>
do you have access to currentUser in the profile page ejs successfully?
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.
yes. though this will be shown on the home. yes I just noticed it formatted like that. my b
home.ejs:
home route in index.js:
I think I can do the calculation inside home.ejs. It will just take lots of ejs tags.
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)
I know that's not the issue you're describing but I got snagged on the error message
actually that might solve the issue, now that I'm looking more deeply. Let me know!
Is this resolved?
no still working on it
just need to figure out how to sort array of objects by one of the properties