RESOLVED - Query for See a Particular Field

Hello I ask for help for this thing, I need to show the result of a query in a particular field I tried looking at this code.

The search is done by an input field called #inputTest which looks for the result in my table the corresponding entry in the ‘code’ column. I want to show the corresponding field in the same row of the table found on the column called ‘redirect’

export function buttonTest_click() {
wixData.query(‘RedirectCerimonie’)
.eq(‘code’, $w(‘#inputTest’).value)
.find()
.then(res => {
let items = results.items;
console.log(items);
let item = items[7];
let redirect = item.redirect;
console.log(redirect);
});
}

I hope someone can help me

Hi,

In your code, you’re looking at the 7th row of the returned results from the query. Is this really what you want to do? What are you trying to do?

Yisrael

Thanks for your help, no what I try to return and the 7 column of the row found with the query, which indicates the ‘redirect’ field of my table. I do not know how to give that result

Thank you

I had a feeling that that’s what you were trying to do. :upside_down_face:

Fields (columns) aren’t accessed by index number. Just think what would happen if you made changes to the collection and the order of the columns changed. Pow! Bam! Things would break.

What you want to do is use the name of the column to get the desired field. So, if the column that you want is called thingie, then you would write:

let field = item.thingie; 

Here’s what your code currently does:

This line in your code retrieves the items that were returned by the query:

 let items = results.items;

This line in your code gets the 7th item (row) that was returned by the query:

let item = items[7];

And this line gets the column (field) called redirect:

let redirect = item.redirect;	

Now, what results do you expect to get from the query?

Yisrael

Thanks I understood many things now, so not having 7 lines of a specific search the result will be undefine.

So I corrected the code like this:

export function buttonTest_click() {
wixData.query(‘RedirectCerimonie’)
.eq(‘code’, $w(‘#inputTest’).value)
.find()
.then(res => {
let items = results.items;
let item = items[0];
let redirect = item.redirect;
console.log(redirect);
});
}

So I look in the first line found that it should be the only column called ‘redirect’, and then check the result with the console.log
But I do not get the result because I have this error that I do not understand why there is: “results is undefine”

Hmm, sorry, missed this…

Change
.then(res => {
to
.then(results => {

results will then no longer be undefined.

It works thank you so much it was a problem I was trying to solve for a long time. You have been a great help.

I only ask you to explain to me why you’ve changed

Change
.then (res => {
to
.then (results => {

What is the reason for this change, to understand

Thank you

Take a look at these lines:

You are trying to use the results of the query using the object named results:
let items = results.items;

However, you are using the object called res as the returned results:
.then (res => {

Simply, the names don’t match. :smiling_face:

Thanks again, you have been a great help.

Have a nice day

Glad I could help. Have fun!

Please try to help me try to save results in the database in another collection

Hi Yisrael,

Any way to adapt this to display all results in the column that match the query string? For some reason it will only show one of the results for me.

Thanks in advance!

–Anney