exporting calculated data to collection using afterQuery

Hi first post to the forum — woot woot

I am a noob to the coding world, but am trying to hack it together. I’m running into issues trying to run calculations on a collection — BLUF: I’m trying to count the total number of books I sell in a preorder campaign and link it to progress bars & inputs on my site to motivate customers to help me reach my goal.

Problem: I have a collection Breakdown with fields: startingQty (#), product (ref), copsinPkg (#)

The product field references the Stores/Product collection — I need to pull the quantityInStock data (from Stores/Product) for each respective product ( qtyinStock ) to run an equation to create a new field called pkgbooksSold , but I can’t figure out a way to pull the quantityInStock from Stores/Products.

Once I get pkgbooksSold I will aggregate that field and link it to the progress bars & inputs on my site ( I mention this to say that I tried doing an afterQuery on the Stores/Products collection, got the pkgbooksSold number I’m looking for, but can’t aggregate that and can’t figure out how to move that to another collection where I can aggregate it)

I’ve referenced this post: https://www.wix.com/velo/forum/coding-with-velo/need-help-with-afterquery-hook but am still having trouble.

Here’s my current code in the data.js file:

import wixData from ‘wix-data’ ;

export function Breakdown_afterQuery ( item , context ) {
var qtyInStock ;

  **return**  wixData . query ( "Breakdown" ) 
  . include ( "product" ) 
  . eq ( "quantityInStock" ) 
  . find () 
  . then (( results ) => {    
          qtyInStock  =  results.items [ 0 ]. quantityInStock ; 
              item.pkgbooksSold  = ( item.startingQty  -  qtyInStock ) *  item.copsinPkg ;  // Add a calculated field "pkgbookssold"       
              console . log ( item );  
              **return**  item ;   
. catch ( ( error ) => { 
    let  errorMsg  =  error.message ; 
    let  code  =  error.code ; 


Thanks. Any help is much appreciated.