Automatic Form Fill for Members

I have a submission page where members who are logged in can submit their username and a comment. Problem is, they have to type their username in each time. According to
https://www.wix.com/corvid/reference/wix-users.html#currentUser you can automatically fetch the logged in member’s username using the find id then data query (see the FAQ question on how can i get the user’s name". When I set up the code I get a sequence error on the wixData.query line. Does anyone know what the problem is or why this is happening and how i can possibly auto fill the logged in member’s username?

import wixUsers from ‘wix-users’;
$w.onReady(function () {
let userId = user.id;
wixData.query(“Members/PrivateMembersData”) \
.eq(“_id”, wixUsers.currentUser.id) \
.find() \
.then( (results) => { \
lastName = results.items[0].lastName; \
} );

1 Like

Hi,

There were a few problems here.

  • The backslashes at the end of the lines were not needed.

  • You need to have an import statement at the top for wixData.

  • “User” wasn’t defined and then wasn’t used. You don’t have to use a variable on that.

  • In the “then” clause, it sounds like you want to assign the login email to an input box, but maybe you somehow tie a username to the login email.
    It would look more or less like this:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
  wixData.query("Members/PrivateMembersData") 
  .eq("_id", wixUsers.currentUser.id) 
  .find() 
  .then( (results) => { 
     $w('#input1').value = results.items[0].loginEmail; 
   });
} ); 

wow thank you so just to clarify for future views of this post, I wanted to auto fill a form submission with the logged in member’s username. Here is the exact code used:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( function () {
wixData.query(“Members/PrivateMembersData”)
.eq(“_id”, wixUsers.currentUser.id)
.find()
.then( (results) => {
$w(‘#input1’).value = results.items[0].nickname;
});
} );

All thanks goes to Anthonyb

Works great but how can I have this so it can also auto populate other form fields with first name, last name and membership id (held in a membersid data set)

@mathieuzelceski You could insert a console.log to see what data it actually returns. When previewing it, you will find the array in the developer console at the bottom. Then populate the other form fields like the example above.

import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {
  wixData.query("Members/PrivateMembersData") 
  .eq("_id", wixUsers.currentUser.id) 
  .find() 
  .then( (results) => { 
     console.log(results.items[0]);
   });
} );

Thank you! Works an absolute dream.

I have one last question. How about if I want to use a different collection named membershipid and a field in that data called membershipid? I have a load of reference membership numbers stored there.