# Custom reviews collection referencing products and member id for wix stores product

**URL:** <https://forum.wixstudio.com/t/custom-reviews-collection-referencing-products-and-member-id-for-wix-stores-product/60971>\
**Category:** Ask the community\
**Tags:** wix-members-area, question, wix, code, wix-stores\
**Created:** [July 18, 2024, 6:53am UTC](https://forum.wixstudio.com/t/custom-reviews-collection-referencing-products-and-member-id-for-wix-stores-product/60971 "2024-07-18T06:53:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olayiwola\_Lanre](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/olayiwola_lanre/32/94503_2.png) [@Olayiwola\_Lanre](https://forum.wixstudio.com/u/Olayiwola_Lanre)\
**Post date:** [July 18, 2024, 6:53am UTC](https://forum.wixstudio.com/t/custom-reviews-collection-referencing-products-and-member-id-for-wix-stores-product/60971/1 "2024-07-18T06:53:27Z")

</div>

Please i need the backend code for the reviews collection to insert the current product and current member into the referenced fields in the collection.

product page Frontend Code  
"$w.onReady(async function () {

```
let thisProduct;

```

populateReviewRepeater();  
});

async function populateReviewRepeater() {  
try {  
const reviews = await getReviews(thisProduct?.\_id);  
const totalRating = reviews?.reduce((total, review) =\> total + review.rating, 0);  
const averageRating = totalRating / reviews?.length;  
$w(‘#reviewsCount’).text = `Based on ${reviews?.length || 0} Reviews`;  
$w(“#allRatings”).rating = reviews?.length ? +averageRating.toFixed(1) : 0;  
$w(“#allRatings1”).rating = reviews?.length ? +averageRating.toFixed(1) : 0;  
$w(‘#ratingsDisplay2’).rating = +averageRating.toFixed(1);  
$w(‘#ratingsDisplay2’).numRatings = reviews?.length;  
$w(‘#ratingsDisplay2’).expand();  
$w(“#avgRatings”).text = `${+averageRating.toFixed(1) || 0}`;  
$w(“#reviewsSection”).expand();  
if (reviews?.length) {  
$w(“#reviewsRepeater”).data = reviews;  
$w(“#reviewsRepeater”).expand();  
}  
} catch (error) {  
console.error(“Error populating review repeater:”, error);  
}  
}

```
$w("#addReviewButton").onClick(async () => {
    try {
        if (authentication.loggedIn()) {
            await wixWindow.openLightbox("Review Popup", thisProduct);
            populateReviewRepeater();
        } else {
            authentication.promptLogin({
                mode: "login",
                modal: true
            });
        }
    } catch (error) {
        console.error("Error opening review popup:", error);
    }
});

$w("#reviewsRepeater").onItemReady(($item, itemData) => {
    $item("#ratingsDisplay").rating = itemData?.rating || 0;
    $item("#content").text = itemData?.content || '';
    $item("#reviewerAvatar").src = itemData?.picture || "https://static.wixstatic.com/media/b188ac_f7f23439a7ba4acf92e103e362fa4275~mv2.jpg";
    $item("#reviewerName").text = `${itemData?.firstName || itemData?.loginEmail}`;
    $item("#reviewDate").text = `${itemData._createdDate.toLocaleDateString()}`;
});

```

LIGHTBOX CODE

"import wixData from ‘wix-data’;  
import { currentMember, authentication } from ‘wix-members’;  
import wixWindow from ‘wix-window’;

//Validates the input fields.

function validateFields(fieldIds) {  
fieldIds.forEach((fieldId) =\> {  
$w(fieldId).onCustomValidation((value, reject) =\> {  
if (!value) {  
reject(“Required Fields”);  
$w(fieldId).updateValidityIndication();  
}  
});  
});  
}  
//Submits a review.

async function submitReview() {  
try {  
$w(‘#loader’).show()  
const fieldIds = [“#ratingsInput”, “#contentInput”];  
validateFields(fieldIds);

```
    const validity = fieldIds.filter((fieldId) => !$w(fieldId).valid);
    if (validity.length === 0) {
        const product = wixWindow.lightbox.getContext();
        const member = authentication.loggedIn() ? await currentMember.getMember() : null;

        const newReview = {
            content: $w("#contentInput").value,
            rating: $w("#ratingsInput").value,
        };

        const newReviewItem = await wixData.insert("Reviews", newReview);
        if (newReviewItem) {
            await wixData.insertReference("Reviews", "member", newReviewItem._id, member._id);
            await wixData.insertReference("Reviews", "product", newReviewItem._id, product._id);
            $w("#successMessage").show();
            $w('#loader').hide()
            wixWindow.lightbox.close();
        }
    }
} catch (error) {
    $w('#loader').hide()
    console.error("Error submitting review:", error);
}

```

}

$w.onReady(function () {  
$w(“#submitButton”).onClick(submitReview);  
const product = wixWindow.lightbox.getContext();  
$w(“#title”).text = `Review ${product.name}`  
console.log(“product”, product)  
});  
"
