Referencing a collection within a collection

I am fairly new to using the Velo API, but I’m trying to do something that quite possibly has already been done.

I have a collection with users and account numbers.
I have a collection with orders by users using a reference field.

I want to display the orders the user has based on the referenced account number.

The trick is that only orders can be entered by site admins via a dashboard page, so using the “owner” field won’t work and I can’t figure how to make data visible based on account numbers.

This is as far as I got coding, but this won’t even display the matching account numbers. I can see all the orders if I comment this section out.

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
    $w("#dataset3").onReady(() => {
        let accountNumber = $w("#text88").text;
        wixData.query("Orders")
            .eq("accountNumber", accountNumber)
            .find()
            .then((results) => {
                let  allResults = results.totalCount;
                console.log(allResults);
                console.log(accountNumber);
                if (allResults > 0) {
                    $w("#table1").show();
                }
                else{
                    $w("#table1").hide();
                    
                }
            });

    }); 
});

Thanks for any help!

Hi,
I’m currently not at my laptop so i won’t be able to help to much at this time but…
The first thing i see that is wrong is this
let accountNumber = $w ( “#text88” ). text ;
The text88 wont have any text yet since the page is just loaded.
What data should be there and how is the data loades to the text88 and where?

Kind regards,
Kristod

Kristof,

#text88 is the user account number displayed by #dataset2

I added a console.log(accountNumber); after let accountNumber = $w ( “#text88” ). text ;

and the correct value was returned.

Oke,
2 things that i think might be the case.

Do you have read permission for your database collection?
ifnot, change it in the database settings so you can access it.

You are getting a Number from the dataset and put it to a string since text88 is a texfield.
Then you are searching the number using a string while it has to be a number.

So maybe try to use

.eq("accountNumber",Number(accountNumber))

Hope it works.

Kind regards,
Kristof

So the account numbers are technically text because they are formatted as such: IG00000 so that bit of code didn’t work.

The “orders” collection permissions are as follows:
I have it set to “Members - Only Content”
Read Content: Site Member
Create Content: Admin
Update Content: Admin
Delete Content: Admin

If I remove the code trying to compare the account numbers, I see the entire list of orders. So I don’t think it’s a permissions issue.

Oke and do you get any errors when you run this code?

@volkaertskristof No. I get the expect account number returned in the console when I enter console.log(accountNumber);

@volkaertskristof I wanted to let you know I solved this issue. The accountNumber reference actually pulls the system id even though the primary item is set to a different field. The easiest way for me to get this working was to place a text box hidden behind another object that pulled the ID from the accounts collection. Once I did that, everything worked and only the orders tied to that account work. It may not be the “right” way to do it, but it works. Here’s the code.

$w("#dataset3").onReady(() => {
        let accountNumber = $w("#text137").text;
        
        wixData.query("Orders")
            .eq("accountNumber", accountNumber)
            .find()
            .then((results) => {
                let totalResults = results.totalCount;
                
                if (totalResults > 0) {
                    $w("#table1").rows = results.items;
                }
                else {
                    $w("#table1").collapse();
                    $w("#text138").show();
                    $w("#text139").show();
                }

            });
});