I am trying to retrieve the first and last name of a user and display it on a text element. I am doing this using the getCurrentItem() function and it was working fine, until it didn’t anymore.
I have checked previous forum posts saying that it should be in the ready() function of the dataset, but it is and so I am not sure why this is not working. It is always pulling up the name of the first item from the collection, instead of the user who is currently logged in.
Here is my code:
$w . onReady ( () => {
$w ( “#dataset1” ). onReady ( () => {
let firstName = $w ( “#dataset1” ). getCurrentItem (). title ;
let lastName = $w ( “#dataset1” ). getCurrentItem (). lastName ;
$w ( “#text63” ). text = firstName + " " + lastName + " - Viewer" ;
} );
});
Can anyone please tell me why is this not working?
How does a visitor arrive at this page? To what collection is dataset1 connected? How is the current item set before arriving at this page?
You say that it was working before. What did it do? How was it used?
Thank you for your reply.
Visitor arrives to this page by logging in → visiting their account page → clicking on a link within their account page → taken to this page.
dataset 1 is connected to a collection called ‘Viewers’ with custom use permissions allowing anyone to read, create, update, delete content.
Not sure what you mean by how is the current item set before arriving at this page.
Before, it was on the same page but it pulled the First and Last names of the currently logged in user, which I used to show in the text element. Now it just brings up the first entry from the collection ‘Viewers’
@houseviewingservice You need to set the current item of the dataset to the current user. See the article Displaying Items Added by the Logged-in Member for details on how to do this.
Current User is now a defunct method of getting data from a database. I had actually spent 12 hours the other day figuring it all out (partly because Promises were still a mystery to me). The new way to get this data is the get() function:
import wixData from 'wix-data';
// ...
wixData.get("database", "elementid")
.then( (results) => {
let firstname = results.title;
let lastname = results.lastName;
//________________________________
$w("#text63").text=firstName+ " " +lastName+ " - Viewer" ;
//Do all of your work with these variables here.
//_________________________________
} )
.catch( (err) => {
let errorMsg = err;
} );
@yisrael-wix Thank you for the suggestion. I didn’t know about this so thank you for sharing it with me. Unfortunately, I can’t use this because I am not using the Wix Members Area and I don’t have it installed on my site. I made everything custom.
@josephchancewatkins Thank you so much for your reply. It looks promising I just have one question.
I would assume “elementid” is the id of the currently logged in user. If so, how would you get that?
The new currentMember.getMember function to get that id doesn’t seem to work with this. let user = user.id doesn’t work as well as let userId = wixUsers.currentUser.id doesn’t work either.
Anyone know how to add the id? Please let me know