How to filter a dataset on the basis of a referenced field

Hey there!!
So I have two databases : Type and subtype.
Items in one field of the “type” database act as referenced items in the Subtype database.
Now i want to filter a dataset that is connected to the Subtype database and find all such entries in the Subtype database that have the same referenced item(from the type database).
How do I do this??
This is the code i tried:
import wixData from ‘wix-data’ ;
import {local} from ‘wix-storage’ ;
import wixLocation from ‘wix-location’ ;
$w.onReady( function (){
let newFilter=wixData.filter()
let types=local.getItem( “type” )
let typeso
let typeid
let typeid2
wixData.query( “Types” )
.eq( “types” ,types)
.find()
.then((result)=>{
typeso=result.items[ 0 ]
typeid=typeso.type
typeid2=typeso._id
});
//newFilter=newFilter.contains(‘type’,ty)
newFilter=newFilter.contains( “type” , typeid2)
$w( “#dataset1” ).setFilter(newFilter)

$w( "#text14" ).text= "Type:-" +local.getItem( "type" ) 

});
But the filtering does not take place and all the items in the Subtype database are displayed in the repeater.
How do I achieve filtering the dataset connected to the Subtype database so that all the entries carrying the same referenced item as one field entry are obtained??

Have you tried to declare newFilter outside of OnReady function?

no,
but why do you suggest doing so??

@stackupsiteshsumanyu Because " let newFilter=wixData.filter()" is only good for the block scope within OnReady, not outside of it.
https://www.w3schools.com/js/js_let.asp