• Node.js Video Tutorials

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: []
nodejs_url_module.htm
Advertisements