Returning variable from response on the end of function in javascript [duplicate]

I have this function:

function settingTextLan(text) {
  fetch("i18n.json")
    .then((response) => response.json())
    .then((data) => {
      text = data[language].translation.test;
    });
  return text;
}
console.log(settingTextLan());

And i want return “text” in current location, But now i get “Undefined”. Later i will need to return few values that contains certain texts from JSON file and call them in different places.

How to do it to call that and have access in other places?

Bash script calls a .js file with a function that should update global variable, but it doesn’t

I have a updateGlobalVar.js file which looks as following:

var globalVar = 111;

async function setGlobalVar() {
    globalVar = 222;
}

setGlobalVar();

Also, I have a bash script, which retrieves that globalVar value, and looks as following:

#!/bin/bash

npm run /updateGlobalVar.js
GLOBAL_VAR=$(grep -Po '(?<=^var globalVar = ).*?(?=;)' ./updateGlobalVar.js)

echo $GLOBAL_VAR

The problem here is the bash script prints out initial 111 instead of an updated 222.

The way I understand that flow is:

  • npm run /updateGlobalVar.js runs updateGlobalVar.js (and I am sure it does);
  • function setGlobalVar() is called (and I am sure it is);
  • globalVar should be now updated and printed with a new 222 value. But it doesn’t.

Moreover, when I modify my file to “debug” using the console.log() within the updateGlobalVar.js file like:

var globalVar = 111;
console.log(globalVar);

async function setGlobalVar() {
    globalVar = 222;
}

setGlobalVar();
console.log(globalVar);

It returns 111 in both console.log(globalVar) cases, which makes me think that globalVar is never updated.

Looks like I miss something fundamental regarding the work with global variables, still am struggling to figure that out.

May this be somehow related to the asynchronous nature of the setGlobalVar() function?

get object response value

I got a response from the backend the response is an object how can I get the arrays from the object I tried to use Object entries but I can’t get the arrays can you please help meenter image description here

How do I delete an item from An array stored on local storage-JavaScript

Am trying to implement a feature where an item is to be deleted from local storage when a user clicks a button. The item is part of an array stored in local storage. However am not able to
here is how the item is saved

const favBtns=meal.querySelectorAll(".fav-btn i")
    const images = document.querySelectorAll('.meal-header > img')
    favBtns.forEach((favBtn,i)=>{
      favBtn.addEventListener("click", ()=>{
        favBtn.classList.toggle("active")
        console.log(images[i].src, images[i].alt)     
        myFavMeals.push({src: images[i].src, alt: images[i].alt})
        localStorage.setItem("myFavMeals", JSON.stringify(myFavMeals))
        showMeal
    })     
})

here is how am trying to delete the item

   function clearfav(){
        const clears=document.querySelectorAll("i#clear.fa.fa-window-close")
      clears.forEach(clear=>{
        clear.addEventListener("click",()=>{
          let favmeals= JSON.parse(localStorage.getItem("myFavMeals"))
          favmeals.forEach((favmeals,i)=>{
            favmeals.splice(favmeals[i],1)
            showMeal()
           })
        })   
      })
      }

how to get the pure text and selected option in a p-tag?

After selecting 5 and 2, does any solution for how could get a string output like: 5 greater than 2 ?

<p class="showTxt" id="showTxt">

  <select size="2">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
  <p> greater than</p>
  <select size="1">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
</p>

How to edit row name while using ag-grid react

i am using AG-Grid in react, I want to rename the row title count which appear automatically while using aggregation. the photo will help you to understand better.enter image description here

look at the row which says “United States” and then (1109) which is count of matching pairs i want to remove that count from there. can anyone help me in this.

Is there a way to proxy api calls in electron?

Here is my situation

  1. In my web renderer, I send all api calls based on baseUrl: ‘/api’
    enter image description here

  2. In my electron main, I serve all my web static files based on ‘File://’ protocal
    enter image description here

So the problem is

How do I proxy all 'file:///api/*' api calls to a specific url, like proxy to 'http://myapiserver.com/api/*'

How to update content in a page using webhook?

I’m trying to update content in an active loaded page using webhook response.

Earlier, I used to trigger the function using setTimeout for every 5 secs but noticed its kind of increasing server load. So, now whenever there is a change on the database a webhook will be triggered through which I will update the content on the loaded page without refreshing it.

But how do I tell a webhook URL to change/update content on another page.?

so far code goes as :
PAGE#1:

<div id="loader_div">Some content</div>
<script>
function loadlink(){
    $('#loader_div').load('loading.php',function () {
         $(this).unwrap();
    });
}

loadlink(); // This will run on page load

setInterval(function(){
    loadlink() 
}, 5000); // this will run after every 5 seconds
</script>

loading.php

<p>some other content that i got from database</p>

Now the webhook URL is loading.php which gets the new update from the database, but how do I trigger the ‘loadlink();’ on the page1 without refreshing it and using set timeout..?

Please do not downvote the query, I’m trying my best to ask properly as per guidelines. You got to give time to rookies.

Any help is appreciated.

cesium load WMTS, tile map offset

var provider = new Cesium.WebMapTileServiceImageryProvider({
    url: 'xxx.xx.xxx',
    layer: 'RESPL_2018',
    style: 'default',
    format: 'image/png',
    tileMatrixSetID: 'xxx',
    tilingScheme: new Cesium.WebMercatorTilingScheme(),
});

This is how the map is loaded.I successfully loaded a map,but the latitude of the loaded map is offset by + 50 degrees. Is there any way to reset the starting point of the WMTS loading!

enter image description here

How to copy individual files into folder directory at relevant search depth reading from Google Sheet

I managed to find a way to create a folder directory from reading a Google Sheet linked below.

https://docs.google.com/spreadsheets/d/1qgDd8PEmHSYz5IsN9banYBjbmAyoLLVe3WMnOvmHdlE/edit#gid=0

Now, my predicament is to transfer individual files into those newly created folders and the relevant search Depth. I thought this would be possible; however, I am now thinking it isn’t hence the limited code below.

This is because for each file (Column C), I would need to read the source parent folder ID (Column D) and then convert this to the destination folder equivalent recorded in Column F. I’m not sure how I can go about doing this? Moreover, should the Google Sheet be sorted into folders and files in a first instance (Column C), ideally the solution should still work when files and folders are out of order.

I know this is not the best solution; however, I am looking for a solution I can understand in order to advance my understanding.

function copyFiles() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName('Details');
  const range = ws.getDataRange();
  const values = range.getValues();
  values.forEach(function(row, index) {
    if (row[2] == 'File'){

    } else {

    }
    })
}

Thank you in advance

Can revealing module pattern be done without IIFE like this and serve same purpose?

My understanding of revealing module pattern is as follows:

CODE #1

const iifeModulePattern = (function () {
  const secretSeed = 999; // ← private
  const logSecretCode = function(){console.log(secretSeed+1)};
  return {methodForPublic : logSecretCode};
})();

iifeModulePattern.methodForPublic();

So far I hope I’m not wrong. My question is:

  1. Won’t the following Code #2 serve same purpose?
  2. If it does, why is Code #1 popular than Code #2?
  3. If it doesn’t, what’s the difference?

CODE #2

const modulePattern = () => {
  const secretSeed = 999; // ← private
  const logSecretCode = function(){console.log(secretSeed+1)};
  return {methodForPublic : logSecretCode};
};

modulePattern().methodForPublic();

I won’t store “secret” codes (like passwords) in this way. The above codes are just examples.

Console logging in HackerRank’s JavaScript

Just trying to run through the basic Hacker Rank practice tests.

They often give you an array of integers (arr) as input. Supposedly. However, using their “JavaScript(Node.js),” attempting to console.log(arr) at the outset of the function to verify the data type simply returns undefined.

Any array methods I use, whether syntactically correct or not, return undefined. Trying to console.log(“hello”) returns undefined. Its pretty infuriating that the solution is some basic difference. Lil help?

Thanks.

How to draw the 3D cube using React.Js and Three.Js with given JSON

I have JSON given below. Using this JSON need to create the 3D Box inside the 3D outline cube as shown in given image.

{
  "request_id": "614bd0e3de7f3745708a9fa4",
  "completed_time_UTC": "2022-02-06T20:56:19Z",
  "tracks": [
    {[![enter image description here][1]][1]
      "purchase_orders": ["1153511"],
      "vehicle_id": "be8c578a-c59c-4348-8148-50cfbeb5a6cd",
      "vehicle_type": "TRAILER_48T",
      "number_of_pallets": 14,
      "pallets": [
        {
          "purchase_orders": ["1153511"],
          "pallet_id": "fe76b310-d751-48eb-84f8-3ea47c7a94bd",
          "pallet_type": "BLANCA",
          "number_of_skus": 65,
          "skus": [
            {
              "sku": "17503006575454",
              "length": 32,
              "width": 27,
              "height": 26,
              "position": [0, 54, 26]
            },
            {
              "sku": "17503006575454",
              "length": 32,
              "width": 27,
              "height": 26,
              "position": [0, 0, 26]
            },
            {
              "sku": "17503006575454",
              "length": 26,
              "width": 27,
              "height": 26,
              "position": [64, 59, 78]
            },
            {},
            {
              "sku": "17503006575454",
              "length": 32,
              "width": 27,
              "height": 26,
              "position": [59, 54, 80]
            }
          ]
        },
        {
          "purchase_orders": ["1153511"],
          "pallet_id": "e693e8bd-e841-4a05-8912-aa0e837ba256",
          "pallet_type": "BLANCA",
          "number_of_skus": 65,
          "skus": [
            {
              "sku": "17503006575454",
              "length": 32,
              "width": 27,
              "height": 26,
              "position": [0, 0, 0]
            },
            {
              "sku": "17503006575454",
              "length": 32,
              "width": 27,
              "height": 26,
              "position": [0, 0, 26]
            },
            {},
            {
              "sku": "17503006575454",
              "length": 32,
              "width": 27,
              "height": 26,
              "position": [58, 54, 78]
            }
          ]
        }
      ]
    }
  ],
  "schemaName": "http://www.schema.org/logistics/1"
}