Accessing different values of the JSON array from wix.Data query

How to do access the city: in the below screenshot using javascript code?

image

For the purpose of the example I’ll pretend the array logged is called arr, accessing the city would be using arr[0].city, that is assuming you want the city specifically of the first item in the array

That didn’t seem to work but I may be missing something else. What I am trying to do is I have a database of all city zip codes. I’m searching the zip codes to get the matching city. Then I need to access that.

wixData.query("Import793")
			.eq("zipcode", cityzip)
			.find()
			.then((results) => {
			console.log(results.items)
		    City = results[0].city;
			console.log(City)
			
			})
			.catch((err) => {
				console.log(err);
			
			})

When I try the above code I get this error:

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

The array you logged was results.items, not results

const city = results.items[0].city

By the way, you should probably implement the case in which no results were found

1 Like