Working with promises

I am trying to create a new property using data hook. The function I have logs the results successfully but when I try to assigned the result of the function to the new object, it doesn’t work. it shows "totalCoaches":{"isFulfilled":false,"isRejected":false} I am not sure why. Would anyone know how to solve this please? Thanks!

import wixData from ‘wix-data’;

export function CoachingAreas_afterQuery(item, context) {

const findNumCoaches = (id) => {
return wixData.queryReferenced(“CoachingAreas”,id,“coachName”)
.then(result => {
let totalCoaches = result.totalCount;
console.log(totalCoaches);
return totalCoaches;

})
}
item.totalCoaches = findNumCoaches(item._id).then(result => result)

return item;
}

Hey
Try this, I think your last promise is not waited for in your code.

import wixData from 'wix-data';

export function CoachingAreas_afterQuery(item, context) {

 const findNumCoaches = (id) => {
 return wixData.queryReferenced("CoachingAreas",id,"coachName")
            .then(result => {
 let totalCoaches = result.totalCount;
 console.log(totalCoaches);
 return totalCoaches;
            })
    }
 return findNumCoaches(item._id).then(result => {
 item.totalCoaches = result;
 return item;
    })

}
1 Like

Thank you!!