Retrieve field data from collection and put it in input form

So far I’m able to get the user ID and use it in an input field on a form with the code I have. How do you do the same for any other field? like for example the owner field, or the image field, or any other?
This is what I have:

import wixUsers from ‘wix-users’;
$w.onReady(function () {
let user = wixUsers.currentUser.id;
$w(‘#input1’).value = user;
});


It automatically fills in the first input on the form.
I want to do the same on the second and third inputs but I want to retrieve other fields, like username, and image, etc…


How do you do that? Help please!

1 Like

So I came up with this:

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

$w.onReady(function () {
let user = wixUsers.currentUser.id;
$w(‘#input1’).value = user;

   const currentUser = wixUsers.currentUser; 
currentUser.getEmail().then(email => { 
    wixData.query('Afiliados').eq('email', email).find()  
    .then(result => {  
      const item = result.items[0];  
      $w('#input2').value = item.nombreDeUsuario; 

});
});
}
);

it does retrieve the field from the collection but for some reason when I click submit on the “comentar” button it doesn’t do anything…
HEEEEEEELLLLPPPP! thanks.

Hi,

wixUsers only have the following data:


let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true
let userRole = user.role;       // "Member"

As you can see, there is no picture there.

Are you saving the picture on your own database?

Liran.

Yes, I’m saving the picture and everything else to my own database. I added some code to the second input and it does retrieve the field from the collection but for some reason when I click submit on the “comentar” button it doesn’t do anything…
This is what I added to the second input:

  const currentUser = wixUsers.currentUser; 

currentUser.getEmail().then(email => {
wixData.query(‘Afiliados’).eq(‘email’, email).find()
.then(result => {
const item = result.items[0];
$w(‘#input2’).value = item.nombreDeUsuario;
});
});
}
);

The button won’t work because your code is inside $w.onReady() which runs when the page is loaded.
You need to set an onClick function for the button.
Read a bit here .

Liran.

I added this to the code and still no luck :

export function button8_onclick() {
$w(“#dataset1”).save();

}

And I did check the onclick option on the properties panel.

:frowning:

Originally, I had the code for my first input inside $w.onReady() and the submit button did work, it wasn’t until I added the second and third inputs that the damn button stopped submitting to my database. I don’t understand why it doesn’t work now even after I added export function button8_onclick() {
$w(“#dataset1”).save();

Help…

For some reason when I click the submit button only the first input is sent to my collection and the other fields for inputs 2 and 3 appear blank in the collection.
This is what I have:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady(function () {
let user = wixUsers.currentUser.id;
$w(‘#input1’).value = user;
let currentUser = wixUsers.currentUser;
currentUser.getEmail().then(email => {
wixData.query(‘Afiliados’).eq(‘email’,email).find()
.then(result => {
let item = result.items[0];
$w(‘#input2’).value = item.nombreDeUsuario;
$w(‘#input3’).value = item.image;
}
);
});
});

HELP! :frowning:

I see you have an image there,
How would you expect an image to show in an input box?
Maybe you should put an image instead?

What is the type of the other field?

Liran.

Did you get it working? Is the email being sent to your collection?

I am having the same issue. I am retrieving the userId and Email but only userID gets sent to the collection. Email gets sent only sometimes. It’s very flaky.

Following as trying to do basically the same and having issues (I am not a coder by any stretch). Wanting to have the user Name, contact info and photo fill into form.

Hi Jeff,
Have you tried using the adding dataset and then connecting the form fields to the dataset?

I found someone on another forum post who commented a solution and when I used the code it helped me. I still had to make sure the value was set when uploading into the database but I think this should get everyone in a better place.

https://www.vorbly.com/Vorbly-Code/WIX-CODE-PASSING-DATA-BETWEEN-PAGES