- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
NodeJS urlSearchParams.getAll() Mehtod
The NodeJS urlSearchParams.getAll() method of URLSearchParams class is used to get all the values of a specified name in the query string.
The URLSearchParams API provides methods that gives access to read and write the query of a URL. This class is also available on the global object.
Let’s consider a YouTube URL (‘https://www.youtube.com/watch?t=RSUgBMA-8Ks?t= TSUgRRMA-95’), where the portion after the ‘?’ is known as the query segment. In this query, (t) is the name, and (RSUgBMA-8Ks) is the value. Together it forms a name-value pair. Likewise, there are two name-value pairs in the query string, both are having the name (t) and the values are different. So if use the getAll() method on the name (v), it returns all the values it.
Syntax
Following is the syntax of the NodeJS URLSearchParams.getAll() method
URLSearchParams.getAll(name)
Parameters
name: This specifies the name of the parameter to return.
Return Value
This method returns an array containing all the values of the given name passed as a parameter. If there is no pair whose name is name, an empty array is returned.
Example
If the name to be searched is present multiple times in the query string, the NodeJS urlSearchParams.getAll() method returns the values of all its occurrences as an array.
In the following example, we are trying to get all the values of the key named ‘title’ in the query string.
const url = require('node:url'); const MyUrl = new URL('https://www.tutorialspoint.com?title=1&title=2&body=3&title=4'); console.log("URL: ", MyUrl.href); const Params = new URLSearchParams('title=1&title=2&body=3&title=4'); console.log("Query string: " + Params); console.log("Trying to get all values for the key 'body'....."); console.log("The value is: " + JSON.stringify(Params.getAll("title")));
Output
As we can see in the output below, the NodeJS getAll() method returned all the values of the name (‘title’).
URL: https://www.tutorialspoint.com/?title=1&title=2&body=3&title=4 Query string: title=1&title=2&body=3&title=4 Trying to get all values for the key 'body'..... The value is: ["1","2","4"]
Example
If the name to be searched is not present in the query string, the getAll() method returns an empty array.
In the example below, we are trying to get the value of the name (‘contactUs’).
const url = require('node:url'); const MyUrl = new URL('https://www.tutorialspoint.com?title=1&header=2&body=3&footer=4'); console.log("URL: ", MyUrl.href); const Params = new URLSearchParams('title=1&header=2&body=3&footer=4'); console.log("Query string: " + Params); console.log("Trying to get all values for the key 'contactUs'....."); console.log("The value is: " + JSON.stringify(Params.getAll("contactUs")));
Output
The getAll() method returned an empty array because the name which is been searched is not present in the query string.
URL: https://www.tutorialspoint.com/?title=1&header=2&body=3&footer=4 Query string: title=1&header=2&body=3&footer=4 Trying to get all values for the key 'contactUs'..... The value is: []
To Continue Learning Please Login
Login with Google