This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 12 months ago.
Below is my GET request. I am trying to retrieve the client_id and redirect_uri parameters.
https://sdkapp.example.com:8443/central-login/index.html?client_id=dtvClient&redirect_uri=https://www.example3.com:443/callback
And then utilize those values, in a embedded js script within the same html page.
Config.set({
clientId: //fetched query parameter for client_id
redirectUri: // fetched query parameter for redirect_uri
});
If this is on the client you can use URL and searchParams
// const url = new URL(location.href); // uncomment and delete next line
const url = new URL("https://sdkapp.example.com:8443/central-login/index.html?client_id=dtvClient&redirect_uri=https://www.example3.com:443/callback"); // for example
const obj = {
"clientId": url.searchParams.get("client_id"),
"redirectUri": url.searchParams.get("redirect_uri")
};
console.log(obj)
// Config.set(obj)
If on the server: for example node also has URL
And here is an answer for php: Get URL query string parameters
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I'm writng a node.js app to read messages from Serial Port. Reading data and logging it into console works fine, althought I'm wondering how to save data value from Serial Port to a variable. I want to pass it further to a MySQL, so I need the data to be stored in variable.
I tried to use global variable, but it keeps saying "undefined". I also tried to pass the value using return in js function, but it doesn't work too. Here's my code:
var SerialPort = require('serialport');
const parsers = SerialPort.parsers;
const parser = new parsers.Readline({
delimiter: '\r\n'
});
var port = new SerialPort('COM10',{
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
port.pipe(parser);
parser.on('data', function(data) {
console.log('Received data from port: ' + data);
});
Please tell me how to store data from parser.on in a variable.
Doesn't variable = data;work?
This question already has an answer here:
How do I parse a URL for a specific Query Paramter in javascript?
(1 answer)
Closed 2 years ago.
I need to fetch authorization code from the URL . It is present as a query string parameters.
When I run the belowo URL
https://XXX.authenticaion.com/oauth/authorize?response_type=code&client_id=sb!t113
It redirects to
http://localhost:8080/?code=8wFgU1GJo3
I need to parse the localhost URL and fetch the code.
Please help on how to retrieve the code
Code :
const url = 'https://XXX.authenticaion.com/oauth/authorize?response_type=code&client_id=sb!t113'
const config = {
method: "GET"
};
const response = await fetch(url ,config);
console.log('Response Text...............'+response.text())
You could use plain js URL web api to create URL object and then get the code value.
const url = 'http://localhost:8080/?code=8wFgU1GJo3'
const code = new URL(url).searchParams.getAll('code')
console.log(code)
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Difference between count() and find().count() in MongoDB
(5 answers)
Closed 4 years ago.
I need get amount of documents in db (mongodb) . I tried get this value to my var like this:
var unique = collection.find({email: email}).count();
and like this:
var unique = collection.find({email: email}).toArray();
unique = unique.length;
But when I try to see this number in console, that show me 'undefined' :/
Whats wrong?
P.S sry for my english
From the docs. It's db.collection.count. Don't use find.
Returns the count of documents that would match a find() query for the
collection or view. The db.collection.count() method does not perform
the find() operation but instead counts and returns the number of
results that match a query.
Example Usage:
const MongoClient = require('mongodb').MongoClient;
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'users';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
const db = client.db(dbName);
const email = 'foo#bar.com';
const query = { email: email };
const options = {};
db.collection.count(query, options, (err, result) => {
// handle error
if (err) {
console.log(err);
}
// do something with result
console.log(result);
});
});
Here is a basic example of how count might work. I assume you are using the mongodb npm and not mongoose or other mongo wrappers like it. The way I would use this in a project is by making the connection to your mongodb its own module so that it can be reused with other queries. That way you don't have to wrap every query with a connection.
If you want to get all the docs of a collection, use:
db.collection.count
If you want to get all the docs of a collection by a field, use:
collection.find({email: email}).toArray(function (err, items) {
console.log(items.length);
})
This question already has answers here:
How to get GET (query string) variables in Express.js on Node.js?
(26 answers)
Closed 4 years ago.
I use express framework and react on front app for manage http request on node app. A have method :
app.get('/api/matches', async (req, res) => {
console.log(req.originalUrl); // /api/matches/
console.log(req.query); // {}
...
when I use url like http://localhost:3000/matches?id=123 I expect to get id inside req.query object but instead I get empty {} object. Also I tried to check how express see url using originUrl object, its return without query ?id=123.
You need to use your URL like http://localhost:3000/api/matches/?id=123. Notice that api word. This is because your GET route has /api/matches and request will look for path /api/matches. Doing that change will work for you. Then with that change you will be able to get req.query as {id: 123}
How do I get page URL with parameters from a service worker?
I have tried self.registration.scope but that doesn't include the parameters.
I'm not clear as to whether you're asking about getting the service worker script's URL, or the URLs of all of the client pages that are open under the service worker's scope. So... here's how to do both:
// Get a URL object for the service worker script's location.
const swScriptUrl = new URL(self.location);
// Get URL objects for each client's location.
self.clients.matchAll({includeUncontrolled: true}).then(clients => {
for (const client of clients) {
const clientUrl = new URL(client.url);
}
});
In either of those cases, once you have a URL object, you can use its searchParams property if you're interested in the query parameters:
if (url.searchParams.get('key') === 'value') {
// Do something if the URL contains key=value as a query parameter.
}
You can get waiting.scriptURL or active.scriptURL, pass result to URL() constructor, get .search property of object
navigator.serviceWorker.register("sw.js?abc=123")
.then(function(reg) {
const scriptURL = reg.waiting && reg.waiting.scriptURL || reg.active.scriptURL;
const url = new URL(scriptURL);
const queryString = url.search;
console.log(queryString);
}).catch(function(err) {
console.log("err", err);
});
ServiceWorker.scriptURL can give you the URL and its parameters as well.
Then, the following step is to get a ServiceWorker object, and it depends on where you would like to use the URL parameters.
In the worker script, use self.serviceWorker.scriptURL. e.g.
const searchParams = new URLSearchParams(self.serviceWorker.scriptURL);
In the page script, use scriptURL with navigator.serviceWorker.ready. e.g.
const serviceWorkerRegistration = await navigator.serviceWorker.ready;
const activeServiceWorker = serviceWorkerRegistration.active;
const searchParams = new URLSearchParams(activeServiceWorker.scriptURL);
However, you might want to get a registration object from register API, but the above code snippet should work as well.