How to get current page's resourceId or contextId?

I’m having trouble with
wix comments v2 api.
It said I can query the comments using resourceId , but in frontend, I’m using CMS dynamic page and don’t find any get resourceId api to filter the fetched comments.

What I tried,

  • I tried checking with Wix Location link but it’s different from the reosurceId,

Can anyone guide me how to get resourceId or contextId in frontend?

You can use queryComments() from the Comments API to retrieve all comments from the Wix Comments App.

Here is a backend web module you can run to get a list of all comments:

import { webMethod, Permissions } from "@wix/web-methods";
import { comments } from "@wix/comments";
import { auth } from "@wix/essentials";

const wixCommentsAppId = "91c9d6a7-6667-41fb-b0b4-7d3b3ff0b02e";


export const queryComments = webMethod(Permissions.Anyone, async () => {
    const elevateQueryComments = auth.elevate(comments.queryComments)
    try {
        const response = await elevateQueryComments(wixCommentsAppId).find();
        return response;
    } catch (e) {
        console.error(e);
        return { error: true, e };
    }

});

Please note we retrieved the required appId from the following article in the documentation.

In the items returned by this method (representing each comment) you’ll see a field titled resourceIdwhich can be used to get comments associated to a particular dynamic item page path.

1 Like

I got that part already, what I want is to filter the comment of current page. Can you provide any example for me? I need the frontend filter the result sent from backend using “contextId” or “resourceId”
I’ve tried filtering by current wixdataset’s _id but it’s being different. Here is my code,

//Frontend Filtering using current page's CMS item's _id to check if it's match with resourceId. But unfortunately it's not,
import wixLocation from 'wix-location';
import { queryCommentsForPage } from 'backend/commentsv2.web'; // adjust your backend file path

$w.onReady(function () {
    console.log("=== DEBUG INFO ===");
    console.log("Full URL:", wixLocation.url);
    console.log("Path array:", wixLocation.path);
    
    // Get the actual item ID from dataset
    if ($w('#dynamicDataset')) {
        $w('#dynamicDataset').onReady(() => {
            const currentItem = $w('#dynamicDataset').getCurrentItem();
            console.log("Dataset current item:", currentItem);
            
            if (currentItem) {
                const itemId = currentItem._id; // 75d550e2-3285-4d78-86d5-061ca572e869
                console.log("Using item ID as resourceId:", itemId);
                
                // Fetch comments for this specific page
                queryCommentsForPage(itemId)
                    .then((response) => {
                        console.log("Comments response:", response);
                        
                        if (response.error) {
                            console.error("Error fetching comments:", response.e);
                        } else if (response.items && response.items.length > 0) {
                            console.log("✅ SUCCESS! Found comments for this page");
                            displayComments(response.items);
                        } else {
                            console.log("❌ No comments found for this page yet");
                        }
                    })
                    .catch((error) => {
                        console.error("Error calling backend:", error);
                    });
            }
        });
    } else {
        console.log("❌ No dynamicDataset found");
    }
});

// Function to display comments
function displayComments(comments) {
    console.log(`Displaying ${comments.length} comments:`);
    comments.forEach((comment, index) => {
        console.log(`Comment ${index + 1}:`, {
            id: comment._id,
            author: comment.author?.name || 'Anonymous',
            content: comment.content?.text || 'No content',
            date: comment._createdDate,
            resourceId: comment.resourceId
        });
    });
}

//Backend
import { webMethod, Permissions } from "wix-web-module";
import { comments } from "@wix/comments";
import { auth } from "@wix/essentials";

const wixCommentsAppId = "91c9d6a7-6667-41fb-b0b4-7d3b3ff0b02e";

// Get all comments
export const queryAllComments = webMethod(Permissions.Anyone, async () => {
    const elevateQueryComments = auth.elevate(comments.queryComments);
    try {
        const response = await elevateQueryComments(wixCommentsAppId).find();
        console.log("All comments:", JSON.stringify(response, null, 2));
        return response;
    } catch (e) {
        console.error(e);
        return { error: true, e };
    }
});

// Get comments for specific page/resource
export const queryCommentsForPage = webMethod(Permissions.Anyone, async (resourceId) => {
    const elevateQueryComments = auth.elevate(comments.queryComments);
    try {
        const response = await elevateQueryComments(wixCommentsAppId)
            .eq("resourceId", resourceId)
            .find();
        console.log(`Comments for resourceId ${resourceId}:`, JSON.stringify(response, null, 2));
        return response;
    } catch (e) {
        console.error(e);
        return { error: true, e };
    }
});

I tried using both wix-comments package and wixcommentsv2.

:melting_face:

A user recently posted the solution to a similar problem, try going through their code and see if you can find something that points you in the right direction:

I tested your queryCommentsForPage() method and it works as expected.

I believe the error occurs when the item id is passed from the dataset (currentItem) into the method.

 const currentItem = $w('#dynamicDataset').getCurrentItem();
 console.log("Dataset current item:", currentItem);

At the moment it seems to be passing the item ids from the data passed to the dataset.

Verify that the resourceId you see in queryAllComments() for the associated item’s comment thread matches what is being passed into the queryCommentsForPage() method.

1 Like