How to get nested fields' values from database - Backend?

Hello again,

I was trying to check whether a particular product was purchased by the current logged in user, my last query has failed, and I don’t know why? I might be missing something.

The code is on the Product Page , and the Product ID is already stored, here’s my code:

Backend Code

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

export async function alreadyPurchased(productId) {
    let options = {
        "suppressAuth": true,
        "suppressHooks": true
    }

    let queryOrders = await wixData.query("Stores/Orders")
        .eq("buyerInfo.id", wixUsers.currentUser.id)
        .find(options)

        console.log(queryOrders)

        if (queryOrders.length > 0) {
            for (let i = 0; i < queryOrders.length; i++) {
                let lineItems = await wixData.query("Stores/Orders")
                    .eq("number", queryOrders.items[i].number)
                    .eq("buyerInfo.id", wixUsers.currentUser.id)
                    .find(options)

                let queryItems = await lineItems.items[i].lineItems;
                let queryItemsLength = await lineItems.items[i].lineItems.length;

                console.log(queryItemsLength, "Query Items Length")
                console.log(queryItems, "Query Items")

                for (let j = 0; j < queryItemsLength; j++) {
                    let products = await wixData.query("Stores/Orders")
                        .eq("number", queryOrders.items[i].number) // <<= The orders returned from "queryOrders" query
                        .eq("buyerInfo.id", wixUsers.currentUser.id) // <<= Match the order buyer ID with the current logged in user ID
                        .eq(`lineItems[${j}].productId`, productId)
                        .find(options)
                        .then((result) => {
                            console.log(result, "Products Result")
                            if (result.items.length > 0) {
                                let results = true
                                return results
                            } else {
                                let results = false
                                return results
                            }
                        })
                        .catch((err) => {
                            let errorMsg = err;
                            console.warn("Search failed: Unable to retrive data")
                        });

                let productsLength = await products.items.length;
                let productsList = await products.items.j;

                console.log(productsLength, "Products Length");
                console.log(productsList, "Products List");
                }
            }
        } else {
            console.info("Customer has no orders yet");
            return false
        }
}

Frontend Code

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

import { alreadyPurchased } from 'backend/store-data.jsw';
import { getMemberID } from 'backend/secureMember.jsw';

let name;
let product;

$w.onReady(async function () {
    product = await $w('#productPage').getProduct();   
    await isItemPurchased(product._id);

});

function isItemPurchased(productId) {
    return alreadyPurchased(productId);
}

My search in the second For loop always failed, I can’t figure out the reason? Any ideas? And now I get an error on live website console:

“mainr.js:185 GET https://…wixsite.com/…/api/wix-sm/v1/authorize/4eXXXXXf-0cae-4714-b9ce-39XXXXXc5X1/pages 403 (Forbidden)”
“Failed to load resource: the server responded with a status of 403 (Forbidden)”

Pretty hard for me :pensive:

Help is appreciated.

any updates on this? I keep getting the same one

mainr.js:188 GET https://www.passion.moda/api/wix-sm/v1/authorize/e6d8ca74-3659-49bd-8f5e-a3343be05883/pages 403