Query collection to navigate to dynamic page

I have been trying to figure this out all day and I’m sure the answer is right in front of me but I just can’t see it. I have two collections: Job Seeker & Job Seeker Resumes. On a ‘JobSeeker’ dynamic page you submit a form that fills the ‘JobSeekerResumes’ database and populates the ‘resumeId’ field with the logged in users email as a reference field.

If I connect a button on a static page or in a ‘JobSeeker’ dynamic page to the $w(‘#JobSeekerResume’) dataset it just navigates to the last profile added to the database and not the one of the logged in user.

I am trying to navigate there using code but I am not that familiar with wixLocations. This is what I have so far but I can’t figure out how to reference that resumeId field correctly and connect it back/associate it with the correct user profile and URL.

export function btnOnlineRes_click(event) {
let currentItem = wixUsers.currentUser.id;
wixData.query( “JobSeekerResumes” )
.find()
.then((results) => {
console.log(results);
if (results.items.length > 0 ) {
let url = results.items[ 0 ];
console.log(url);
wixLocation.to(/job-seeker-resumes/${currentItem.url})
}
})
}

Hi Erin :raised_hand_with_fingers_splayed::raised_hand_with_fingers_splayed:

I’m sure you’re getting a 404 error page not found, you’re using the user ID to access a property called url inside it, obviously the ID of the user does NOT have properties since it’s string, not an object.

Also, you’re assigning the url variable the the first returned object from the query, not the field itself.

I understand what you want to do, you want to get the user ID and get the resume that match the logged in user and redirect him/her to the resumes page.

Try doing this:

 export async function btnOnlineRes_click(event) {
     // Get the current user
     let user = wixUsers.currentUser;
     
     // Query the database to find this user's resume;
     await wixData.query("JobSeekerResumes")
         .eq('owner', user.id)
         .find()
         .then((result) => {
             let resume = result.items[0];
             wixLocation.to(`/job-seeker-resumes/${currentItem.url}`)
         })
 })

Notice the blue part, this line is so important to get resume that belongs to the current user, otherwise you’ll send the user to the resume page of the first user’s resume.

.eq('owner', user.id)

owner is the field key that hosts the ID of users in your collection, you need to change it to yours.

Hope that helped~!
Ahmad

Hey Ahmad :hugs: Thanks for your help. When I add this code it tells me the await can’t be outside of the async function, but when I change it to an async function I get a parsing error on the brackets second from the bottom, am I using the async incorrectly?

async function btnOnlineResume_click(event) {
let user = wixUsers.currentUser;
await wixData.query( ‘JobSeekerResumes’ )
.eq( ‘JobSeeker’ , user.id)
.find()
.then((result) => {
let resume = result.items[ 0 ];
[wixLocation.to(wixLocation.to(/job-seeker-resumes/${currentItem.url})](wixLocation.to(wixLocation.to(/job-seeker-resumes/${currentItem.url})
*parsing)
parsing error })
})

Hey Erin

I already did edit the answer, pay attention to the first line

 export async function btnOnlineRes_click(event) {

Also, the wixLocation line was edited, I wrote it twice before, here’s the correct one.

wixLocation.to(`/job-seeker-resumes/${currentItem.url}`)

Also, when replying to someone, please reply to their answers like I did here, don’t make the reply look like an answer.

Apologies, I didn’t refresh my page before replying so I didn’t see your edits. Am I correct in saying that I should substitute ‘currentItem’ with ‘resume’ as it’s asking me to define currentItem?

No worries Erin :blush:

I did write the whole code, so you can delete whatever code you have and paste mine.