Hi,
I need to create an array for all rows from table1 that do not have an instance in table2 (by ID) Afterwards, I need to manipulate those rows.
I tried creating the function with the code below, as an async function and I put an “await” next to the first wixData.query (table1) but I needed another “await” next to the second wixData.query (for table2) but it gives me an error that i cannot put an await inside a non async function.
I couldn’t figure out how to do it… how can i have two await inside the same function.
let TableArray = [];
wixData.query(“table1”)
.limit(1000)
.descending(“_createdDate”,“userId”)
.eq(“active”, true )
.find()
.then( (results) => {
if (results.items.length !== 0) {
results.items.forEach( (row) => {
wixData.query(“table2”)
.eq(“table1”,row._id)
.find()
.then( (results2) => {
if (results2.items.length === 0) {
TableArray.push( {“field1”: row.title,
“field2”: row.userId,
“field3”: row.active,
});
}
} )
. catch ( (gifterr) => {
console.log(gifterr); //see item below
} );
}
}
} )
And another question… is there a better way to get all rows from table 1 that do not have an instance in table 2 than the way i did it?
Anatarad,
Obviously, I can’t begin to test this with your tables and query conditions, but this approach worked on my own tables. The point is to make a separate async function and call it from a button or another function. With a for loop you go through each of the results of the first query and run the second query.
export async function NotIn(){
let TableArray = ;
const results = await wixData.query(“table1”).limit(1000).descending(“_createdDate”,“userId”).eq(“active”, true ).find()
if (results.totalCount > 0) {
let items = results.items,nRecs = results.totalCount,nCnt = 0;
for (nCnt = 0; nCnt < nRecs; nCnt++) {
const results2 = await wixData.query(“table2”).eq(“table1”,items[nCnt]._id).find()
if (results2.totalCount === 0) {
TableArray.push( {“field1”: items[nCnt].title,
“field2”: items[nCnt].userId,
“field3”: items[nCnt].active,
});
}
}
}
console.log(TableArray);
}
export function button1_click(event) {
NotIn();
}
Thank you very much anthony!
For unknown reasons, it still didn’t bring it correctly so I separated it to another different function and now it works. I have one function that makes an array out of table1 and then another function that makes another array from the rows not in table2
so it works and thank you very much!
Glad to know that you have it working!