Retrieving a Single Record from a Collection
Learn how to query and retrieve a single record directly from a collection using Corvid (a.k.a. Wix Code).
This example shows you how to retrieve a single shape record from a collection called #Shapes and displays the colour of the shape record found. Try the example below.
Try it!
iptSearch
Type a shape to begin!
#txtMessage
Shape | Colour |
---|---|
Circle | Red |
Circle | Red |
Rectangle | Green |
Circle | Blue |
Square | Green |
Triangle | Red |
Circle | Blue |
Square | Green |
Triangle | Red |
Triangle | Blue |
Circle | Blue |
Triangle | Green |
Square | Red |
Circle | Red |
Circle | Red |
Square | Green |
Triangle | Red |
Triangle | Blue |
Circle | Blue |
Triangle | Green |
Square | Red |
Circle | Red |
Circle | Red |
Triangle | Red |
Triangle | Blue |
Circle | Blue |
Triangle | Green |
Circle | Red |
Circle | Blue |
Rectangle | Green |
Triangle | Red |
Square | Green |
Square | Green |
Triangle | Red |
Rectangle | Green |
Circle | Blue |
Add an onKeyPress event listener to the #iptSearch text box. It will pass the desired shape entered to the findShape function after a two second delay to ensure the use has finished typing. The findShape function executes a wixData.query on the #Shapes collection. If the desired shape is not found, a message is sent to #txtMessage advising the user. If the desired shape is found, then the colour is retrieved from the record and displayed to the user in #txtMessage.
The toTitleCase function converts the users shape input to title or proper case, which matches the case in the #Shapes collection - as the wixData.query function is case sensitive.
Page Code