queryLocations( )
Creates a query to retrieve a list of locations.
Description
The queryLocations()
function builds a query to retrieve a list of up to 1,000 locations and returns a LocationsQueryBuilder
object.
The returned object contains the query definition which is typically used to run the query using the find()
function.
You can refine the query by chaining LocationsQueryBuilder
functions onto the query. LocationsQueryBuilder
functions enable you to sort, filter, and control the results that queryLocations()
returns. The functions that are chained to queryLocations()
are applied in the order they are called.
queryLocations()
runs with the following LocationsQueryBuilder
defaults that you can override:
skip
:0
limit
:50
The following QueryLocationsBuilder
functions are supported for the queryLocations()
function. For a full description of the Locations object, see the object returned for the items
property in LocationsQueryResult
.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function queryLocations(): LocationsQueryBuilder
queryLocations Parameters
This function does not take any parameters.
Returns
Return Type:
Was this helpful?
1import { locations } from 'wix-business-tools.v2';2import { elevate } from 'wix-auth';34export async function myQueryLocationsFunction() {5 try {6 const elevatedQueryLocations = elevate(locations.queryLocations);7 const myLocations = await elevatedQueryLocations().find();8 const items = myLocations.items;9 console.log('Locations:', items);1011 return items;12 } catch (error) {13 console.error(error);14 // Handle the error15 }16}1718/* Returns items:19 * [20 * {21 * "_id": "5c6e81a4-5270-4f66-9688-49d1fd9153ef",22 * "address": {23 * "city": "Amsterdam",24 * "country": "NL",25 * "formatted": "CityHub, Bellamystraat, Amsterdam, Netherlands",26 * "location": {27 * "latitude": 52.3672879,28 * "longitude": 4.86709889999999829 * },30 * "postalCode": "1053 BE",31 * "streetAddress": {32 * "apt": "",33 * "name": "Bellamystraat",34 * "number": "3"35 * },36 * "subdivision": "NH"37 * },38 * "archived": false,39 * "businessSchedule": {40 * "periods": [],41 * "specialHourPeriod": []42 * },43 * "default": false,44 * "email": "amsterdam.store@example.com",45 * "fax": "+31 20 765 4321",46 * "name": "Bellamystraat",47 * "phone": "+31 20 123 4567",48 * "status": "ACTIVE",49 * "timeZone": "Europe/Amsterdam",50 * "revision": "1"51 * },52 * {53 * "_id": "e3b2c1ff-2bf2-41da-9f4a-983c8799074a",54 * "address": {55 * "city": "Brisbane City",56 * "country": "AU",57 * "formatted": "Arise, Margaret Street, Brisbane City QLD, Australia",58 * "location": {59 * "latitude": -27.4718535,60 * "longitude": 153.028831261 * },62 * "postalCode": "4000",63 * "streetAddress": {64 * "apt": "",65 * "name": "Margaret Street",66 * "number": "222"67 * },68 * "subdivision": "QLD"69 * },70 * "archived": false,71 * "businessSchedule": {72 * "periods": [],73 * "specialHourPeriod": []74 * },75 * "default": false,76 * "email": "australia.store@examp;e.com",77 * "fax": "+61 2 9876 5432",78 * "name": "Margaret Street",79 * "phone": "+61 2 1234 5678",80 * "revision": "1",81 * "status": "ACTIVE",82 * "timeZone": "Australia/Brisbane"83 * }84 * ]85 */86
1import { Permissions, webMethod } from 'wix-web-module';2import { locations } from 'wix-business-tools.v2';3import { elevate } from 'wix-auth';45export const myQueryLocationsFunction = webMethod(Permissions.Anyone, async () => {6 try {7 const elevatedQueryLocations = elevate(locations.queryLocations);8 const myLocations = await elevatedQueryLocations().find();9 const items = myLocations.items;10 console.log('Locations:', items);1112 return items;13 } catch (error) {14 console.error(error);15 // Handle the error16 }17});1819/* Returns items:20 * [21 * {22 * "_id": "5c6e81a4-5270-4f66-9688-49d1fd9153ef",23 * "address": {24 * "city": "Amsterdam",25 * "country": "NL",26 * "formatted": "CityHub, Bellamystraat, Amsterdam, Netherlands",27 * "location": {28 * "latitude": 52.3672879,29 * "longitude": 4.86709889999999830 * },31 * "postalCode": "1053 BE",32 * "streetAddress": {33 * "apt": "",34 * "name": "Bellamystraat",35 * "number": "3"36 * },37 * "subdivision": "NH"38 * },39 * "archived": false,40 * "businessSchedule": {41 * "periods": [],42 * "specialHourPeriod": []43 * },44 * "default": false,45 * "email": "amsterdam.store@example.com",46 * "fax": "+31 20 765 4321",47 * "name": "Bellamystraat",48 * "phone": "+31 20 123 4567",49 * "status": "ACTIVE",50 * "timeZone": "Europe/Amsterdam",51 * "revision": "1"52 * },53 * {54 * "_id": "e3b2c1ff-2bf2-41da-9f4a-983c8799074a",55 * "address": {56 * "city": "Brisbane City",57 * "country": "AU",58 * "formatted": "Arise, Margaret Street, Brisbane City QLD, Australia",59 * "location": {60 * "latitude": -27.4718535,61 * "longitude": 153.028831262 * },63 * "postalCode": "4000",64 * "streetAddress": {65 * "apt": "",66 * "name": "Margaret Street",67 * "number": "222"68 * },69 * "subdivision": "QLD"70 * },71 * "archived": false,72 * "businessSchedule": {73 * "periods": [],74 * "specialHourPeriod": []75 * },76 * "default": false,77 * "email": "australia.store@examp;e.com",78 * "fax": "+61 2 9876 5432",79 * "name": "Margaret Street",80 * "phone": "+61 2 1234 5678",81 * "revision": "1",82 * "status": "ACTIVE",83 * "timeZone": "Australia/Brisbane"84 * }85 * ]86 */87
This is an example of a page where user searches for locations, and the search results populates a dropdown. The query only returns active locations.
1/****************************************2 * Backend code - query-location.web.js *3 ***************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { locations } from 'wix-business-tools.v2';7import { elevate } from 'wix-auth';89export const queryLocationsByNameStart = webMethod(Permissions.Anyone, async (searchInput) => {10 try {11 const elevatedQueryLocations = elevate(locations.queryLocations)12 const result = await elevatedQueryLocations()13 .startsWith('name', searchInput)14 .eq('status', 'ACTIVE')15 .find();1617 return result.items;18 } catch (error) {19 console.error(error);20 throw new Error(error);21 }22});232425/*************26 * Page code *27 ************/2829import { queryLocationsByNameStart } from 'backend/query-location.web';3031$w.onReady(() => {32 $w('#getBranchLocations').onClick(async () => {33 const searchName = $w('#search').value;34 const locations = await queryLocationsByNameStart(searchName);3536 const dropdownOptions = locations.map((location) => {37 return {38 label: location.name,39 value: location._id40 }41 });42 $w('#locationsDropdown').options = dropdownOptions;43 });44});45