-2

I have created a small data collection for my wixsite with three fields FirstName (field id is firstName) LastName (field id is lastName) Email (field id is email) I have a button on my webpage (#button1). All i want is to access the record with particular email and display the values of individual fields. I have tried using wix-data.query object. Can anyone plz help?

import wixData from 'wix-data';

// ...

wixData.query("quiz2")
.eq("mail","ishuuw@gmail.com")
  .find()
  .then( (results) => {
    //using a text box to check if results have got any record
$w("#text71").text=results.totalCount
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164

2 Answers2

0

You need to query based on the field id. Which is what I think you understand based on the question.

So the problem you have is that your query isn't testing any of your stated field ids.

your .eq("mail","ishuuw@gmail.com") test probably needs to use email and not mail so your query should look like this

wixData.query("quiz2")
.eq("email","ishuuw@gmail.com")
.find()
.then((results) => ...

Then you simply need to test for results.length > 0. This is the length of the returned query result. The results are then found in the items array if you have any...

let firstFoundRecord = results.items[0];
SteveC
  • 141
  • 3
  • I ran the following code ........i used 'email' field key in the query but mistyped it here in the question. This is how my code looks.......but it still doen't run.............export function button1_click(onClick,button1) { wixData.query("quiz2") .eq("email","ishuuw@gmail.com") .find() .then( (results) => { //using a text box to check if results have got any record $w("#text71").text=results.totalCount } ) .catch( (err) => { let errorMsg = err; } ); } – Ishuta Wankhede Dec 22 '18 at 11:14
  • I also wanted to know if the items array is two dimensional or not.....and then how can I access individual field values of a particular result? – Ishuta Wankhede Dec 23 '18 at 16:41
  • In answer to your question about the items array being two dimensional, no the array you receive back in items is a one dimensional array of objects. Each object contains a row in your “quiz2” data collection. Each column is accessed using the field key that is created for you when you name your column. See the documentation here: https://support.wix.com/en/article/wix-code-about-database-collections. So if you name a column “Question Number” you will get a field key of questionNumber. To access the value of that column from items you would use results.items[itemIndex].questionNumber. – SteveC Dec 26 '18 at 22:54
  • Can you please check if the following code is correct? – Ishuta Wankhede Jan 11 '19 at 17:53
  • export function button1_click(onClick,button1) { $w("#text71").text="a" ///This works well wixData.query("quiz2") .find() .then( (results) => { let firstItem = results.items[0].email; //email is the FiledKey for a //Column in Data Collection named 'quiz2' $w('#text72').text=firstItem; //This fails to work } ) .catch( (err) => { let errorMsg = err; } ); } – Ishuta Wankhede Jan 11 '19 at 17:55
  • let a=results.totalCount; $w('#text71').text=a.toString(); this works well now – Ishuta Wankhede Jan 11 '19 at 19:15
  • thanks for the help steve! it would be a big help if you could provide a wix code snipppet that identifies a particular record in data collection using a query and shows its individual column values in textboxes on a webpage. – Ishuta Wankhede Jan 11 '19 at 19:18
  • An now i am sufferin with another problem...........my code responds in preview mode, but nothing works on the published site..............is there any way to debug the code? – Ishuta Wankhede Jan 11 '19 at 19:20
  • Hi Ishuta: Have you synchronize your data collection sandbox to live? Go to the data collection schema page and click the Sync menu option. – SteveC Jan 17 '19 at 09:16
  • i had problems with synchronization itself.......later came to know that the preview mode uses sandbox database, while live website uses live database, and both should be accessible (set to be accessed by anyone) and synchronized. – Ishuta Wankhede Jan 17 '19 at 20:51
0

Can you be more precise about what doesn’t run? It looks like you have connected your button1_click() évent handler to a button element in its property panel. Is the function still connected? Did the function name in the button1 onClick property change by any chance?

SteveC
  • 141
  • 3