Pass result from one query to the next query

Evening folks.

I have a page linked to two datasets.

I also have a 1.5 second delay in the code below to stop the searches happening too quickly. That part works fine.

I query the first dataset and return two results to text on the screen. That works.

What I am trying to do is to pass a result from the first query, fkArticleCode, into the second query such that it returns two more results that I again can pass to text on the screen. This is the part that will not work. The text on the screen for the second results does not update.

I have followed the suggestions here but cannot seem to get my code working.

https://www.wix.com/corvid/forum/community-discussion/passing-results-of-one-query-to-another-query

Can anybody help?

import wixData from 'wix-data';

export function articleCodeInput_input(event) {
 let debounceTimer;
 let lastFilterTitle;
 let articleCode = $w('#articleCodeInput').value;
 let code = $w('#codeInput').value;
 if (debounceTimer) {
    clearTimeout(debounceTimer);
    debounceTimer = undefined;
  }
  debounceTimer = setTimeout(() => {
    filter($w('#articleCodeInput').value);  
  }, 1500);
let lastfilterTitle;
function filter(title) {
 if (lastfilterTitle !== title) {
//first query
wixData.query('ProcureASSISTv1').eq('articleCode', articleCode).eq('code', code).find()
        .then((result)=> {
 if (result.length > 0) {
 let article = result.items[0];
                $w("#note2").hide()
                $w("#note").show()
                $w('#articleCode').text = article.articleCode;
                $w('#description').text = article.description;
 let fk = article.fkArticleCode;

 //second query
               wixData.query('PAFKonly').eq("customerCode", code).eq('fkArticleCode', fk).find()
        .then((res)=> {
 let article2 = res.items[0];
 $w('#fkArticleCode').text = article2.fkArticleCode;
                $w('#fkDescription').text = article2.fkDescription;
 
   });
            } else {
                $w("#note").hide()
                $w("#note2").show()
            }
        })
lastfilterTitle=title;
}}
}

I am wondering if I should remove the debounceTimer as this delay is causing the second query not to work?

Any assistance is appreciated :grinning:

let query1 = wixData.query('Collection').eq("field",value");
let query2 = wixData.query('Collection');
query1.find().then(res =>
const items = res.items;
return query2.eq("field", items[0].field).find();
}).then(items=>
//whatever
})

Thanks J.D but I don’t understand this line:

cons items = res items

I get “Parsing error. Unexpected token items”.

Does items need to be defined?

UPDATE @J.D.

My full code is below but I still get that parsing error pointing to cons items.

import wixData from 'wix-data';

export function articleCodeInput_input(event) {
 let debounceTimer;
 let lastFilterTitle;
 let articleCode = $w('#articleCodeInput').value;
 let code = $w('#codeInput').value;
 if (debounceTimer) {
    clearTimeout(debounceTimer);
    debounceTimer = undefined;
  }
  debounceTimer = setTimeout(() => {
    filter($w('#articleCodeInput').value);  
  }, 1500);
let lastfilterTitle;
function filter(title) {
 if (lastfilterTitle !== title) {

let query1 = wixData.query('ProcureASSISTv1').eq('articleCode', articleCode).eq('code', code);
let query2 = wixData.query('PAFKonly');
query1.find().then(res => {
if (result.length > 0) {
 let article = res.items[0];
                $w("#note2").hide()
                $w("#note").show()
                $w('#articleCode').text = article.articleCode;
                $w('#description').text = article.description;
cons items = res items;
return query2.eq("fkArticleCode", items[0].field).find();
}).then(items=>
 let article2 = res.items[0];
 $w('#fkArticleCode').text = article2.fkArticleCode;
                $w('#fkDescription').text = article2.fkDescription;
 
   });
            } else {
                $w("#note").hide()
                $w("#note2").show()
            }
        })
lastfilterTitle=title;
}}
}

Perhaps J.D. forgot a —> . <----- between res and items

cons items = res.items

Thanks @russian-dima.

I stripped back all my unnecessary code just to get a msall proof of concept working as shown below but it now states “the keyword let is reserved” for line:

let article = res.items[0];

Any suggestions?

import wixData from 'wix-data';

export function articleCodeInput_input(event) {

let query1 = wixData.query('ProcureASSISTv1').eq('articleCode', articleCode).eq('code', code);
let query2 = wixData.query('PAFKonly');
query1.find().then(res =>
let article = res.items[0];
                $w('#articleCode').text = article.articleCode;
                $w('#description').text = article.description;
cons items = res.items;
return query2.eq("fkArticleCode", items[0].field).find();
}).then(items=>
let second = items.items[0];
                $w('#fkArticleCode').text = second.fkArticleCode;
                $w('#fkDescription').text = second.fkDescription;
})

It is better when you ask J.D. for further help, because it is his CODE-CONCEPT.
He can help you better than me.

it should be cons t items = res . items ; NOT cons items = res items