Search Collection and Display Result in Text Object

import wixData from ‘wix-data’;

export function searchResults() {
const selectedClass = $w(‘#dropdown1’).value;
const admissionNumber = $w(‘#input1’).value;

wixData.query('Results') 
    .eq('Class', selectedClass) 
    .eq('Admission Number', admissionNumber) 
    .find() 
    .then(results => { 
        if (results.length > 0) { 
            const studentResult = results[0];  
            $w('#txtName').text = studentResult.Name; 
            $w('#txtEngScore').text = studentResult['English Score'].toString(); 
            $w('#txtMathScore').text = studentResult['Math Score'].toString(); 
            $w('#txtSciScore').text = studentResult['Science Score'].toString(); 
        } else { 
            
        } 
    }) 
    .catch(error => { 
        console.log(error); 
    }); 

}

TypeError: Cannot read properties of undefined (reading ‘name’)

According to the error, the property ‘name’ is undefined.

This is likely because the Query object that is returned by wixData.query() does not return a plain array.

Instead of:

 const studentResult = results[0]; 

Try accessing the first result with the following:

const studentResult = results.items[0];