Passing results of one query to another query

I am in the process of creating a social networking site where a member can like another member’s profile. When they like the profile from a repeater, that like is inserted into the “liked” data collection where there is the column for the owner as well as a reference column for the liked profile.

I am trying to create a page where a member can see all the profiles that liked them. To do this, I attempted to query the liked database. The query finds all the instances of the user’s id in the liked profile column. I am trying to take the owner field from the results and use it in another query for the userprofile collection. So essentially I am trying to filter the profiles data collection using the results (_owner) from the query of the liked data collection. I then want to display those profiles in a repeater.

I am really new to this so I understand the code may be way off, but I can’t find any posts that answered how to query or filter using multiple values.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
let user = wixUsers.currentUser;

$w.onReady(async function () {
    $w("#dataset1").onReady( () => {
        wixData.query("Liked")
        .contains("liked", user.id)
        .find()
        .then( (results) => {
 let item = results.items;

 const people = wixData.query("userProfile")
        .hasSome("_owner", item)
        $w("#repeater2").data = people.items;})
    });
});

Also, if there is a better or easier way to do this, please guide me to that! Thanks in advance!

Hello Fatima,
very good description of your issue, but if you would also give some pics of related databases, it would be much better.

Of course, hope these help!

UserProfile database:

Liked database:

I want a page that’ll show Reeshyal Zeerak’s profile(from the userprofile database) in a repeater since he liked her.

Ok, today a little bit late, but tomorrow again, sorry.

Hi Reeshayal :raised_hand_with_fingers_splayed:

You can do it by two ways,

  1. The first way is to store the first query result in a variable that’s in the same or on an upper scoop of your next query, then access the variable inside the other query.
let query = wixData.query('students').find();

// and here's your second query
wixData.query('teachers').find().then((results) => {
    let firstQueryItems = query;
    let secondQueryItems = results.items;
})
  1. The second way is to nest the two queries inside each others, for example, you want to get the medications of a student by name, you first query the students databse by the name of the student, then you pulled out his medications from the patients database by the ID of that student.
wixData.query('students').eq('firstName', 'Omar').find()
.then((result) => {
    let student = result.items[0]
    let name = student.name;
    
    let medications = wixData.query('patients')
    .eq('studentId', student.id)
    .find((patient) => {
        return patient.items[0].medications;
    })
})

Hope this helps~!
Ahmad

Ok, i think Ahmad will manage this.

Thank you for your quick response Ahmad, but there are some key differences in the two scenarios. First, my initial query is using a reference column. Second, my other query uses not one result from the initial query, but many results. I attempted fixing my code using what you provided, but the issue is still there. I have bolded the parts that are confusing me the most.

wixData.query('Liked').contains('liked', user.id).find()
   .then((result) => {
       let item = result.items;

       let people = wixData.query('userProfile')
          .hasSome('_owner', item)
          .find((results) => {
             $w("#repeater2").data = results.items.people;
                })
        })

The first query should work fine. It’s finding all the rows that have the user’s id in the reference column. The second query is what I’m struggling with since its using many results, not just one.

Hi :raised_hand_with_fingers_splayed:

Do you mean that each person who liked something needs to be queried? If so you need to use a for loop to query each person.

wixData.query('Liked').eq('liked', user.id).find()
   .then((result) => {
      let people = [];
      
      for (let i=0; i<result.items.length; i++) {
         let item = result.items[i];
         
         let person = wixData.query('userProfile').eq('_owner', userId).find().then((results) => {
            let person = results.items[i];
            
            let item = {
               _id: Number(i * 1000),
               details: person
            }
            
            people.push(item)
         })
      }
      
      $w("#repeater2").data = people;
   })

NOTE that when using the user ID, you need to ensure that it match exactly the same ID, so use eq() instead of other functions.

Hope this helps.