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!
This is an old thread that has been answered.
If you have further questions, please open a new thread.
I'm closing it.
hey i got the same problem as claudio ( btw idk why but i can't acces hooks through the content manager so i have to write the code directly in the data.js file i don't know if that affects the proccess ) merci ^^
Hi,
I followed all the the steps mentioned above & its work fine but i'm getting error in my console:
" Cannot find module 'Backend/secureSlug.jsw' in 'public/pages/mplin.js' "
What it is, how to remove?
@Nicolas Forzy
$w.onReady(function () { //TODO: write your page related code here... $w("#UsersPostsBooks").onReady(() => { populateCalculatedFields(); }); function populateCalculatedFields() { const currentAsset = $w("#UsersPostsBooks").getCurrentItem(); const ownerID = currentAsset.authorID; $w("#AuthorID").text = ownerID; } $w("#dataset5").onReady(() => { $w("#dataset5").setFilter(wixData.filter() .eq('_id', $w("#AuthorID").text) ); }); // i get error in this line. anyone knows why? //
and i tried the exact same steps you mentioned after your "evil maniacal laugh" but it just wouldn't work so any ideas about that too will be great.
Good work Nicolas, I was so thinking about just doing the public profile way that I totally forgot about using the data options method through the backend to bypass it all.
https://www.wix.com/corvid/reference/wix-data.html#WixDataOptions
It has all come back to me today what with seeing your code with the supressAuth in it and also reading through Wix CRM backend too while I was looking at something else too.
When should I write code in the backend instead of the client side?
You may want to write your code in the backend in the following cases:
When you don't want visitors to view or manipulate code containing sensitive information such as payment details or API keys.
When you perform security checks or validations that expose private data such as emails and passwords.
When you've set your collection permissions so that a particular role cannot access a collection, but you want visitors with that role to partially access the collection in some manner. In this case you can suppress permission checks using suppressAuth.
So great that you've got it all working for yourself, always a better feeling when you've worked it out for yourself and not have to had somebody else point out your mistake or the way to do it correctly to you.
👍👍
@givemeawhisky
HAHAHAHAHAHAHAHA! (evil maniacal laugh) I SOLVED IT!!!!!!
So, here's what I did, which I checked in the published version of the site and it WORKS!!!
HOOK - saved in backend/data.js
export function Characters_afterQuery(item, context) { // This creates a field named 'authorID' containing the _owner ID of any item returned whenever a query is performed on the 'Characters' collection item.authorID = item._owner; return item; }
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) }
FRONTEND CODE
$w.onReady(function () { $w("#dynamicDataset").onReady(() => { // This launches the hook outlined above when the dataset is ready, because the dynamicDataset is linked to the 'Characters' collection populateCalculatedFields(); }); function populateCalculatedFields() { // This creates the authorID field containing the dynamic item's _owner ID const currentAsset = $w("#dynamicDataset").getCurrentItem(); const ownerID = currentAsset.authorID; // This placed the _owner ID of the dynamic item in a hidden text box as a check $w("#AuthorID").text = ownerID; // 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; let slug = item.slug; // This is a check to make sure the right item is returned console.log(results); // 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; // This inserts the dynamic item owner's slug (also from the Members/PrivateMembersData collection) into a text box on the dynamic page so I can use it to create a link to their profile page that people can access by clicking on the owner's name (through an onClick(event) code using wixLocation.to) $w('#AuthorSlug').text = slug; }) } })
Voila :)
Comments or thoughts on this are very welcome, and a big thank you to everyone who helped me in the above comments because your ideas pushed me into the right direction and, eventually, the solution!
Just make up your own custom login and signup lightboxes and set them to open up instead of using Wix own windows, all of your custom fields that you enter on the signup form will be added to contacts and you can get to it via Wix Crm.
https://support.wix.com/en/article/corvid-enabling-custom-site-registration
https://www.youtube.com/watch?v=QbH8_eudjbE
https://www.youtube.com/watch?v=9eGhGfCiAbo
https://support.wix.com/en/article/adding-custom-fields-to-contacts
You can't use the MembersPrivateData collection as that is read only to the site member author, so only that logged in member can see their own data.
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields
This is where using that public profile form option from earlier and having that separate public profile dataset comes into play as you can simply match the id and email fields in that collection without having to worry about not being able to use the dataset.
You can use Wix CRM amd the email contact function.
https://www.wix.com/corvid/reference/wix-crm.html#emailContact
https://www.wix.com/corvid/reference/wix-crm-backend.html#emailContact
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-contacts
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-members
For more info about login api go here.
https://www.wix.com/corvid/reference/wix-users.html#login
For more info about the register api go here.
https://www.wix.com/corvid/reference/wix-users.html#register
Look at 'Register a user as a site member with registration options' code example and you will see where to put your additional custom fields in your code.
If you are wanting public users to see your members info, then why not create something like this using the members profile setup and instead of having it as a member profile, simply change the pages to be what your members want shown on their public profile.
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area
This then gives the members overall control on what they do want and what they don't want to be made public, like email and telephone numbers for example. They might have used their own in their members profile, however for the public they would prefer their general company email and telephone number instead.
Then the dataset that you used to store their selected choices in can then be used on a public page which displays in a repeater what each member wants to be made public.
Never just test your website using preview mode if you are using anything connected to Members data etc, as you will find that a lot of things in Wix only work fully when viewing on your published site.