.eq related collection search question

Below is my code with problem. It returns 0 result.
First, I use one query to get the input “engFamName”, which works fine. I can get “Aaron” in console.log.
But it is not properly works in the second query.
If I replace “engFamName” in the second query with a string, like “Aaron”, it works as expected.

import wixData from ‘wix-data’;

$w.onReady( function () {
var engFamName = ‘’;

wixData.query("UserInputs") 
    .limit(1) 
    .find() 
    .then( (results)=>{ 
        engFamName = results.items[0].dbFamilyName; 
    }) 

wixData.query("dbcFamNCand") 
    .eq("engName", engFamName )  // Replace engFamName with a string, like "Aaron", it works fine. 
    .limit(2) 
    .find() 
    .then( (results)=>{ 
            console.log(results.items); 
    })

You need to move second query into the {} of the first query to wait for results and execute second query when first is done.

Hello,

The problem is that you should add the second query inside a .then() after you get the results for “engFamName” because it doesnt finish with the first before it starts the second query

Solution would look something like this:

 import wixData from 'wix-data';

$w.onReady(function () {
 var engFamName = '';

    wixData.query("UserInputs")
        .limit(1)
        .find()
        .then( (results)=>{
            engFamName = results.items[0].dbFamilyName;
            return engFamName;
        }).then((name) => {
            wixData.query("dbcFamNCand")
                .eq("engName", name )
                .limit(2)
                .find()                                                  
                .then(  (results)=>{
                    console.log(results.items)
                })
        }).catch((err) => {
            console.log(err)
        })

    

Hope this helps!
Majd

Excellent, both suggestions works well with my question. Thank you Andreas and Majd!