Hello,
I have a site where Members can create Items that can then be viewed by all Users (whether or not they are Members) on dynamic Item pages.
I am trying to show the Member name next to the Item name on the dynamic Item page so that people can know who created the Item. I then want to add a link from the Member name to the Member's profile page (that part is easy).
I have set up a Hook on my Item collection to generate the _owner field text after query on the corresponding Item #dynamicDataset. I place this _owner text into a hidden #AuthorID text box, which I then use as a reference to filter the Members #dataset5 on the page to only the Member whose ID matches the Item's _owner field text. I have a separate visible text box on the page connected to the Member dataset's Name text field to show the Name on the page after filtering. The problem is...
$w.onReady(function () {
//TODO: write your page related code here...
$w("#dynamicDataset").onReady(() => {
populateCalculatedFields();
});
function populateCalculatedFields() {
const currentAsset = $w("#dynamicDataset").getCurrentItem();
const ownerID = currentAsset.authorID;
$w("#AuthorID").text = ownerID;
}
$w('#dataset5').onReady(() => {
$w('#dataset5').setFilter(wixData.filter()
.eq('_id', $w("#AuthorID").text)
);
});
... the above code works in Preview (with me as logged in Member) but not in the Published site (with no-one logged in). All this code is placed within the page's onReady function.
Is there a way to use the _owner field of my Item collection to then display the Member name who created the Item on the Item's dynamic Item page for any User to see?
Any advice would be immensely appreciated here.
Thank you.
Ok, so when you use the code to extract the '_owner' database entry, you then just need to query the Members database to find the 'nickname' (ie. first and last name together) of the user with this ID.
For this, you can use my backend function as a first step:
BACKEND CODE - saved as module secureSlug.jsw
import wixData from 'wix-data'; // This makes sure the function gets the variable from the frontend as 'ownerID' export function getSlug(ownerID) { // This bypasses the permissions check so the query can run for any user, not just the Member who owns the asset let options = { "suppressAuth": true } // This runs the query with the above option in place and finds the Member who's ID matches the _owner ID of the Item displayed on the dynamic page return wixData.query('Members/PrivateMembersData') .eq('_id', ownerID) .find(options) }
Then you need to take the results of that query in the frontend and extract the 'Nickname' that corresponds to that ID:
FRONTEND CODE
Make sure you add: import {getSlug} from 'Backend/secureSlug.jsw'
to the top of your Frontend code window to import the backend function, so you can use it in the frontend.
// This passes the ownerID variable to the backend function above getSlug(ownerID) // Note that the .find instruction is in the backend (where permissions are bypassed through code above) but the .then instruction is here in the frontend, so that I can access the elements of the returned item and define fields to use on the page .then((results) => { let item = results.items[0]; let nickname = item.nickname; // This inserts the dynamic item owner's nickname (from the Members/PrivateMembersData collection) into a text box on the dynamic page to show them as the author of the dynamic item displayed $w('#Author').text = nickname; })
Hope this helps, good luck!