I start in Javascript, and my mission is to retrieve data from an API.
Today I am a step where I have to filter my data according to the user's requests.
Here I want to retrieve the value of the html which is then connected to the switch case in JS. And then fetch the URL of the API according to the value selected by the user.
To make YOU understand, a URL present in the code is equal to a database and I want to select the database to display in my table.
I don't know if I made myself understood. In any case I share my code with you. I've been around my brain, I may have missed something. Any help will be appreciated. Thank's
function boutonSubmit(){
bounceType = document.getElementById('BouncesType').value;
datestart = document.getElementById('dateS').value;
dateend = document.getElementById('dateE').value;
//bounceCode = getElementById('#dateS').value;
var SelectDB = document.getElementById('selectdb').value;
switch(SelectDB){
case 'Desclopinette':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=867XXXXXXXXXXXvGN&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Chrysolum':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=utxyiwq1K8S04WauVIa0&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Acanthius':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=v6PjXXXXXXXXXXX8y682&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Bellapourpre':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=xj530XXXXXXXXXXXRIZlD&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Alibigratis':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=SwyXXXXXXXXXXXdKz7z&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Bullecreatif':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=CN7XXXXXXXXXXXw30llOD&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Cacologia':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=oxjs3XXXXXXXXXXXeGjg&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Elenaparc':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=t02ZrrXXXXXXXXXXXoBm1&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Histoiredepoint':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=juiHFXXXXXXXXXXXSY68y5V&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Iatraliptice':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=LW24pXXXXXXXXXXXTfa28U&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Maitrechic':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=ZbpXXXXXXXXXXXy0S&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Neojaune':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=TA3XXXXXXXXXXXeBJlvc&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'OreilledeLapin':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=uqTXXXXXXXXXXXzf0Eg&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
case 'Princecalme':
`https://api7.esv2.com/v2/Api/Bounces?apiKey=3phXXXXXXXXXXXvO4M4lZ9&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
break;
default:
console.log(`Sorry, we are out of ${Selectdb}`)
}
// Is this the right thing to fetch?
fetch(SelectDB)
.then((response) => response.text())
.then((txtResponse) => {
data = txtResponse;
console.log(data);
data = csvJSON(data);
const tbody = document.querySelector('#user-table tbody');
tbody.innerHTML = '';
data.forEach( (user) => {
const entry = document.createElement('tr')
entry.innerHTML = `
<tr>
<td class="column1">${user.Date}</td>
<td class="column2">${user.Email}</td>
<td class="column3">${user.BounceCode}</td>
<td class="column3">${user.BounceType}</td>
</tr>
`;
tbody.appendChild(entry);
});
}
I suppose you better replace switch case with something more suitable for your case. You can exclude base API URL in constant variable
const DATABASE_API = 'https://api7.esv2.com/v2/Api'
then it makes sence to create separate key=value object with apiKeys
const DB_API_KEYS = {
Desclopinette: 'v6PjXXXXXXXXXXX8y682',
Chrysolum: 'utxyiwq1K8S04WauVIa0',
// and so on...
}
after that you're good to go with full url constructing
const dbApiKey = DB_API_KEYS[SelectDB]
if (dbApiKey === undefined) {
console.log(`Sorry, we are out of ${Selectdb}`)
// throw or something
}
const url = `${DATABASE_API}/Bounces?apiKey=${dbApiKey}&startDate=${this.datestart}&endDate=${this.dateend}&bounceType=${this.bounceType}`
and finally
fetch(url)
Related
looking for a way to automatically update a cell value when a record is created.
I use Airtable, and I would like to automatically assign a developer to the newest record.
Ex. I have 3 developer, christophe/thomas/hugo
Using basic algorithm, but the issue is that when there is a new Biz Dev, we have to manually add him to the arrayBizDev and add a new switch case:
`let arrayBizDev = ["Christophe", "Thomas", "Hugo"]
let nbBizDev = arrayBizDev.length;
let bizDev = "";
switch (nbRdvPris % nbBizDev) {
case 0:
bizDev = "Christophe";
break;
case 1:
bizDev = "Thomas";
break;
case 2:
bizDev = "Hugo";
break;
default:
console.log(erreur);
}
output.set('Business Developper', bizDev);`
I'm working on a simple function that should return the quantity of specific elements in a passed range. I would like to use SWITCH statement but for some reason it doesn't work as I would expect:
function groupResult(range) {
const resultsQuantity = {
na: 0,
fail: 0,
pass: 0,
empty: 0
}
for(let i of range) {
switch(i){
case 'N/A':
resultsQuantity.na++;
break;
case "FAIL":
resultsQuantity.fail++;
break;
case "PASS":
resultsQuantity.pass++;
break;
default:
resultsQuantity.empty++
}
}
return resultsQuantity.na;
}
the function call in the spreadsheet looks like follows:
call of the function in the spreadsheet
but as result I get "0" instead of expected "2"
Use
for(let i of range.flat())
Because the range is a 2d array.
So I have an object build out that has a bunch of methods inside, I want to be able to narrow down some methods and see if there are other ways to do thing, so I will go into more description below:
So I have the following method which returns me a bunch of class names:
function class_names() {
return [
'optanon-category-C0001',
'optanon-category-C0002',
'optanon-category-C0003',
'optanon-category-C0004',
'optanon-category-C0005'
];
}
Now I have another method which basically outputs me a string based on the classname passed inside the parameter:
function classname_output(class_name) {
let output = '';
switch (class_name) {
case 'optanon-category-C0001':
output = 'Strictly Necessary Cookies';
break;
case 'optanon-category-C0002':
output = 'Performance Cookies';
break;
case 'optanon-category-C0003':
output = 'Functional Cookies';
break;
case 'optanon-category-C0004':
output = 'Targeting Cookies';
break;
case 'optanon-category-C0005':
output = 'Social Media Cookies';
break;
default:
output = 'No cookies match the specified class.';
break;
}
return output;
}
Is there a way that I can infuse the two methods into a single method with an object return and then target the object key?
You can have object (dictionary) that maps class names (key) to string (value), and then have a function to return the value if the key exists in the dictionary, or a default "Doesn't exist" string if it doesn't.
const dict = {
'optanon-category-C0001': 'Strictly Necessary Cookies',
'optanon-category-C0002': 'Performance Cookies',
'optanon-category-C0003': 'Functional Cookies',
'optanon-category-C0004': 'Targeting Cookies',
'optanon-category-C0005': 'Social Media Cookies'
};
function check(dict, className) {
return dict[className] ?? 'No cookies match the specified class.';
}
console.log(check(dict, 'optanon-category-C0003'));
console.log(check(dict, 'optanon-category-C0005'));
console.log(check(dict, 'optanon-category-C0000'));
Additional documentation
Nullish coalescing operator
i'm using firebase function.
the code like this.
let transaction_id;
let signature_key;
switch (req.get('Content-Type')) {
case 'application/json':
({transaction_id} = req.body);
({signature_key} = req.body);
break;
case 'application/octet-stream':
transaction_id = req.body.toString();
signature_key = req.body.toString();
break;
case 'text/plain':
transaction_id = req.body;
signature_key = req.body;
break;
case 'application/x-www-form-urlencoded':
({transaction_id} = req.body);
({signature_key} = req.body);
break;
}
console.log(transaction_id);
it still showing undefined.
console log req.body not showing json response.
the i try to JSON.Stringify.
Transaction status is pending {"transaction_id":"1c133665-d73d-45b6-b183-8d263644736b","signature_key":"TzbgFmtPe5qLu1MPcId3MoRcyU%2FBRWHJ6AdQQdLHN6ABy0SpB56F4jYUPFLMemPYL8KhruaQvWWYpAUEXJhcnpQ0Qn%2FHbE0GRwx1Em3Hc%2FWi1mofPUafALccvUmRR3QE","status":"OK","amount":"1020000.00","shipping_address":{},"payment_type":"30_days","transaction_status":"pending","transaction_time":1593094347,"order_id":"P-2020237834543138"}
when i do JSON.stringify it show up above data.
how to remove text outside curly bracket {} so i can convert to json to get value on transaction_id and signature_key?
or how to get the data directly from JSON.stringify output?
If you want to get the the transaction_id and signature_key from the JSON, you can do this
var parsedData = JSON.parse(yourJsonString);
var transaction_id = parsedData.transaction_id
var signature_id = parseedData.signature_id;
Not sure, but is this what you want?
If you want to ommit the data before and after the outer curly brackets, then i would suggest to run an regex
str.replace(/^[^{]*(.*)[^}]*$/,'$1');
And the result can be put into the JSON.prase function.
Summary:
let trx_id = JSON.parse(result.replace(/^[^{]*(.*)[^}]*$/,'$1')).transaction_id;
I'm using Cheerp (https://www.leaningtech.com/cheerp/) to transpile some C++ code into JavaScript.
Is there any option to preserve variable names? Looks like the names get always mangled
Original C++ code:
void myClass::myMethod(int32_T myParam, boolean_T *rty_Result)
{
switch (myParam) {
case Mycase1:
case Mycase2:
case Mycase3:
case Mycase4:
case Mycase5:
*rty_Result = true;
break;
case Mycase6:
*rty_Result = (filter.field1.field2 == 1);
break;
default:
*rty_Result = false;
break;
}
}
Output from Cheerp:
function __ZN8JsBridge12AvailabilityEP9bFilter_Ti(Lthis,filter,myParam){
var tmp0=0;
switch(myParam|0){
case 5:
{
tmp0=filter.a3.i2|0;
return (((tmp0|0)===1?1:0)?1:0)|0;
break;
}
case 1:
case 2:
case 4:
case 6:
case 3:
{
return 1|0;
break;
}
default:{
return 0|0;
break;
}
}
}
I don't find any options in the documentation:
https://github.com/leaningtech/cheerp-meta/wiki
You can try to pass the option:
-cheerp-pretty-code
Source: https://github.com/leaningtech/cheerp-meta/wiki/JavaScript-interoperability#clobbering-names
If that doesn't work, then I'm quite sure that this just unfortunately cannot be accomplished.