How can I call a function with a parameter inside fetch method?

I am working on a function with one parameter, and it should be called with an argument in an Html file, my problem is that this function should loop through fetched data from a local JSON File, the parameter takes a part in the code inside the fetch method, so I cant call the main function, any solutions?!! hope my code shows it clearly

HTML code:

 <div class="on-off-btns">
              <span onclick="showSlide(counter=1)">moon</span>
              <span onclick="showSlide(counter=2)">mars</span>
              <span onclick="showSlide(counter=3)">europa</span>
              <span onclick="showSlide(counter=4)">titan</span>
            </div>

javascript code:

let links = document.querySelectorAll(".on-off-btns span");
let counter = 1;

let myFile = "./data.json";
const showSlide = (s) => {
  fetch(myFile)
    .then((res) => res.json())
    .then((data) => {
      for (let i = 0; i < data.destinations.length; i++) {
        for (let j = 0; j < links.length; j++) {
          if (s > data.destinations.length) {
            counter = 1;
          } else if (s < 1) {
            counter = data.destinations.length;
          }
        }
        images.src = data.destinations[counter - 1].images["png"];
        titles.textContent = data.destinations[counter - 1].name;
        descriptions.textContent = data.destinations[counter - 1].description;
      }
    });
};

Nightwatch: Unable to get the updated array value in the assertion

Using Nightwatch with JS. Below is the Page object which I am using. Under commands, I initialized an empty array, “deletedPlans”. Then further in the code, I’m pushing values to it. In the end, in the assertion, I am trying to print the updated array value in the message, but it prints nothing, taking the array value as empty. Whereas, inside the function where I’ve commented, it prints the array value perfectly. What wrong I am doing?

module.exports = {
url:'/xyz',

elements: {     
    deleteAction:{
        selector: '//span[text()="Delete"]',
        locateStrategy: 'xpath'
    },
    PlanTitleVerify:{
        //some selector
    }
},

commands:[{

    deletePlan: async function (browser){

            var myPage = this
            var deletedPlans =[]
            
            browser 
                .elements('xpath', '//div[@class="ant-card-head-title"]//span', function (elements) {
      
                    elements.value.forEach(element => {
                        let key = Object.keys(element)[0];
                        let ElementIDvalue = element[key];
      
                        browser.elementIdText(ElementIDvalue, function (result) {    
                        let planName = result.value
                        deletedPlans.push(planName) //Adding value to the Array
                
                        console.log("Plan to be deleted:" + deletedPlans) //Here value gets printed      
                    
                        myPage
                          .pause(2000)
                          .click('@deleteAction')
                        })         
                    })           
                })

            myPage.assert.not.elementPresent('@PlanTitleVerify', 'Deleted plans are:' + deletedPlans) //Here value is blank
    },
  }]
};

Listing all values of an object’s property from JSON file

I’m trying to list all ‘languages’ from a JSON file into a <li> using the rest countries API (https://restcountries.com/v3.1/all) that looks like this (example country Malta)

languages: {
eng: "English",
mlt: "Maltese"
},

My code

filteredCountries.map(country =>
          <div key={country.cca2}>
            <h1>{country.name.common}</h1>
            <>Capital: {country.capital}</><br/>
            <>Population: {country.population.toLocaleString()}</>
            <h3>Languages</h3>
            <ul>
              {Object.keys(country.languages).map(lang => { 
              <li key={lang.name}>{lang.name}</li>
            }
              )}
            </ul>
            <img src={country.flags.png} alt='Country Flag'/>
          </div>

I am able to pull the country name, capital, population and flag but unable to list the languages in an efficient manner since every country has a varied number of languages. Using Object.keys doesn’t provide errors but it doesn’t display what I want. I tried using country.languages.map but I get the error of TypeError: country.languages.map is not a function I was able to get the country name and capital using country.name.cca2 and country.capital respectively

Transform javascript string with `import` statement into a function in the browser

I will like my vue application to transform the following string:

const test = 'import { pi } from "MathPie"; function test() { console.log(pi); } export default test;'

into a function that I could call.

import { pi } from "MathPie";

function test() {
  console.log(pi);
}

export default test;

I already tried using eval (even if it’s evil), but it doens’t work because import statement aren’t supported by it.

eval(test)()
> Cannot use import statement outside a module
> Should log '3.14159'

The snippet is for presentation only, I’m aware of Math.PI

My question is how I can evaluate a string with import statement?

Vuejs show keys of unknown nested array

I hava a sample and dynamic array like this:

 nodes: [
        {
          n1: "Foods",
        },
        {
          n4: "Drinks",
          b7: [
            {
              a2: "Beers",
              a4: [
                {
                  a5: "Budweiser",
                  a6: "deneme",
                },
                {
                  a7: "Heineken",
                },
                {
                        a8: "1",
                        a9: "2",
                        
                    },
                    
              ],
            },
            {
              z1: "Wines",
            },
            {
              z2: "Whiskey",
            },
          ],
        },
      ]

This array always changes dynimacally. I want to show this array keys in a tree table. So we should see relationship between parens and child. For example:

n1
n4
  b7
     a2
     a4
        a5
        a6

How do i get real time messages with Axios in Node JS?

import axios from 'axios'

axios.get('http://MYIP/MYURL',{
  auth: {
    username: 'admin',
    password:'888888ov#'
  }
})
      .then(function (res) {
          console.log(res);
         /*  console.log(auth);
          console.log(user); */

          // ... do something with the response
      })
      .catch(function (error) {
          console.log(error);
      })
      .then(function () {
        console.log('teste')
      });

This is my code. Im connecting with this URL. The thing is, it returns me messages constantly,and i would like to get their return with axios in real time. Connect once and listen to the return from the URL in real time until i close the connection. How can i do that?

Debugging a “cannot read properties of undefined” error [closed]

quire often I find myself coming accross this error:

   main.96977df238dbd452662b.js:1 ERROR TypeError: Cannot read properties of undefined (reading 'label')

what will be the best approach to track who’s the problematic key.
In my expreince this usually happens when I have an object in which i “assume” object has property for example: item.label will cause the error above.

  • just a though: wouldn’t it make lots of sense for javascript to present the name of the key that caused the fail?

How can I set default hover::after/active state?

Got a css animation for underlining, but setting:

<button className="btn active">

doesnt seem to have the end of the animation set as default. Can someone hint me what i’m doing wrong? Here’s my CSS:

.btn {
  position: relative;
  text-transform: uppercase;
  color: whitesmoke;
  transition: all 0.5s ease-in;
  font-size: 1.3rem;
  cursor: pointer;
}

.btn:hover {
  color: white;
}

.btn:hover::after {
  transform: scale(1, 1);
  transition: transform 0.3s ease;
}

.btn::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 0.105rem;
  left: 0;
  bottom: 0;
  background: whitesmoke;
  transform: scale(0, 1);
}

.btn:active {
  transform: scale(1, 1);
}

tabs are not triggered after opening a modal window

I need help, I have users who need to edit in a modal window by clicking on the desired one, in the end everything works in the layout, the tabs switch normally, and when I paste the same code into the vue component, the tabs stop working ..

enter image description here

[enter image description here][2]

USER-LIST COMPONENT:

<template>
        <Head title="Dashboard"/>
        <BreezeAuthenticatedLayout>
            <div class="table-home">
                <div class="table-home__header">
                    <div class="table-home__header-title">Пользователи</div>
                    <div class="table-home__options">
                        <div class="table-home__options-item">
                            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M1.12857 4.26839L3.31563 2.12548C3.48647 1.95815 3.7636 1.9582 3.93434 2.12548L6.12132 4.26839C6.39667 4.53812 6.20059 5.00001 5.81195 5.00001H4.5V13.5714C4.5 13.8081 4.30414 14 4.0625 14H3.1875C2.94586 14 2.75 13.8081 2.75 13.5714V5.00001H1.43794C1.04853 5.00001 0.853791 4.53758 1.12857 4.26839ZM7.5625 3.7143H14.5625C14.8041 3.7143 15 3.52243 15 3.28573V2.42858C15 2.19188 14.8041 2.00001 14.5625 2.00001H7.5625C7.32086 2.00001 7.125 2.19188 7.125 2.42858V3.28573C7.125 3.52243 7.32086 3.7143 7.5625 3.7143ZM7.125 6.71429V5.85715C7.125 5.62045 7.32086 5.42858 7.5625 5.42858H12.8125C13.0541 5.42858 13.25 5.62045 13.25 5.85715V6.71429C13.25 6.951 13.0541 7.14287 12.8125 7.14287H7.5625C7.32086 7.14287 7.125 6.951 7.125 6.71429ZM7.125 13.5714V12.7143C7.125 12.4776 7.32086 12.2857 7.5625 12.2857H9.3125C9.55414 12.2857 9.75 12.4776 9.75 12.7143V13.5714C9.75 13.8081 9.55414 14 9.3125 14H7.5625C7.32086 14 7.125 13.8081 7.125 13.5714ZM7.125 10.1429V9.28572C7.125 9.04901 7.32086 8.85715 7.5625 8.85715H11.0625C11.3041 8.85715 11.5 9.04901 11.5 9.28572V10.1429C11.5 10.3796 11.3041 10.5714 11.0625 10.5714H7.5625C7.32086 10.5714 7.125 10.3796 7.125 10.1429Z" fill="#2F3747"></path>
                            </svg>
                            Сортировка
                        </div>
                        <div class="table-home__options-item">
                            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M13.4369 2H2.56315C2.06351 2 1.8114 2.60623 2.16542 2.96026L6.5 7.29549V12.125C6.5 12.3085 6.58955 12.4805 6.73993 12.5858L8.61493 13.8979C8.98484 14.1568 9.5 13.8944 9.5 13.437V7.29549L13.8347 2.96026C14.188 2.60694 13.9376 2 13.4369 2Z" fill="#2F3747"></path>
                            </svg>
                            Фильтр
                        </div>
                    </div>
                </div>
                <div class="table-home__body">
                    <div v-for="user in users" :key="user.id" class="table-home__line" style="display: flex;justify-content: space-between;">
                        <button @click="showModal(user.id)" class="button-show-bid" style="width: 15%">Управление</button>
                        <ModalEdit :ref="'modal_' + user.id" />
                    </div>
                </div>
                <div class="table-home__footer">
                    <div class="pagination">
                        <div class="pagination__count">
                            <div class="pagination__count-title">Выводить по:</div>
                            <div class="pagination__select">
                                <select name="1" id="1">
                                    <option value="10">10</option>
                                    <option value="20">20</option>
                                    <option value="30">30</option>
                                    <option value="40">40</option>
                                </select>
                                <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
                                    <path d="M2.60557 3H10.3944C10.9333 3 11.2027 3.63741 10.8213 4.01097L6.92834 7.82656C6.69222 8.05781 6.30778 8.05781 6.07166 7.82656L2.17875 4.01097C1.79733 3.63741 2.06674 3 2.60557 3Z" fill="#6E7681"></path>
                                </svg>
                            </div>
                        </div>
                        <div class="pagination__list">
                            <div class="pagination__text">1-6 из 1240</div>
                            <div class="pagination__buttons">
                                <div class="pagination__button pagination__button-left">
                                    <svg width="27" height="27" viewBox="0 0 27 27" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path d="M0 3.08571C0 1.38152 1.38152 0 3.08571 0L23.9143 0C25.6185 0 27 1.38152 27 3.08571L27 23.9143C27 25.6185 25.6185 27 23.9143 27L3.08571 27C1.38152 27 0 25.6185 0 23.9143L0 3.08571Z" fill="#ECEFF3"></path>
                                        <path d="M16.4777 21L18 19.355L13.0553 14L18 8.645L16.4777 7L10 14L16.4777 21Z" fill="#2F3747"></path>
                                    </svg>
                                </div>
                                <div class="pagination__button pagination__button-right">
                                    <svg width="27" height="27" viewBox="0 0 27 27" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path d="M0 3.08571C0 1.38152 1.38152 0 3.08571 0L23.9143 0C25.6185 0 27 1.38152 27 3.08571L27 23.9143C27 25.6185 25.6185 27 23.9143 27L3.08571 27C1.38152 27 0 25.6185 0 23.9143L0 3.08571Z" fill="#ECEFF3"></path>
                                        <path d="M11.5223 7L10 8.645L14.9447 14L10 19.355L11.5223 21L18 14L11.5223 7Z" fill="#2F3747"></path>
                                    </svg>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </BreezeAuthenticatedLayout>
</template>

<script>
import BreezeAuthenticatedLayout from '@/Layouts/Authenticated.vue'
import { Head } from '@inertiajs/inertia-vue3';
import Requests from "@/Components/Modules/Requests/List";
import ModalEdit from "@/Pages/Users/ModalEdit";

export default {
    data() {
        return {
            users: []
        }
    },
    components: {
        ModalEdit,
        Requests,
        BreezeAuthenticatedLayout,
        Head
    },
    async mounted() {
        await axios.get('/api/users').then(res => {
            this.users = res.data.data
        });
    },
    methods: {
        showModal(id) {
            let modal_id = "modal_" + id;
            this.$refs[modal_id][0].show(id);
        }
    }
}
</script>

<style scoped>

</style>

MODAL COMPONENT:

<script>
  export default {
    data() {
      return {
        visible: false
      }
    },
    methods: {
      show(index) {
        this.visible = true
      },
      close() {
        this.visible = false
      }
    }
  }
  
  </script>
<template>
    <div v-if="visible">
        <div class="overlay"></div>
        <div class="modal-user">
            <div class="modal-user__container">
                <div class="tabs">
                    <ul class="tabs__button-group">
                        <li>
                            <button class="tabs__toggle active" data-tab="1">Профиль пользователя</button>
                        </li>
                        <li>
                            <button class="tabs__toggle" data-tab="2">Распределение ролей</button>
                        </li>
                    </ul>
                    <ul class="tabs__container">
                        <li class="tabs__tab-panel active" data-tab="1">......</li>
                         <li class="tabs__tab-panel active" data-tab="2">......</li>
                   </ul>
               </div>
             </div>
          </div>  
        </div>
</template>

But in the end, switching does not work

enter image description here

How to sum the values of multiple items in a JavaScript object?

I’d like to sum the values of multiple objects.

I have an array of example 3 items, this varies and can be any number.

_items: 
0: {...}
1: {...}
2: {...}

Each of these item then has the following data:

0: 
dayOfYear: 357
dayOfWeek: 4
noOfUnits: "11"
_id: "12345"
_owner: "12345"
_createdDate: "Thu Dec 23 2021"
year: 2021
_updatedDate: "Thu Dec 23 2021"
week: 51
userId: "12345"
type: "Type 1"

I’d like to add the values of the noOfUnits variable. So for example in item 0 value is 11, item 1 is 2 and item 2 is 5. The total would become 18. This is not limited to 3 items but varies according to the number of items pulled from a database.

Thanks and happy holidays!

Generate a random number with fixed digit without leading zero

I’m using this one liner to generate a random 18 digit number :

Math.floor(100000000000000000 + Math.random() * 900000000000000000)

The problem is that it always contains one or more zeros at the end, and I don’t understand why.

How can I generate a really random number that always contains 18 digits, and without a leading zero?

Advantage of using @Output instead of using @Input which accepts a function in Angular

What is the advantage of using @Output property = new EventEmitter(); decorator with the event emitter instance when the same functionality can be achieved with @Input property: () => void

and then can pass the method from the parent class,

<selector [property]="parentMethod()">

Also, while doing output binding when @Output decorator is used,

<selector (property)="parentMethod()">

Does it internally subscribe to property event emitter instance? This only seems to be beneficial when Angular is using the same instance of a component class at multiple places (does this happen? ), couldn’t think of any other reason.

Would like to understand the work which Angular is doing behind the scene and which of the above-mentioned approach is better and why?

Why is javascript not making the value more than 1 using the +1

I can not make the output number bigger than one when I click a button I a using the +1 operator.
I have search everywhere but just can not find a easy and good solution.

echo <<<_END
    
<p>You have </p><a href="#" id="more" class="list-group-item disabled">0 </a>
<script>
function more(){
           document.getElementById("more").innerHTML = +1;//plus one
        }
</script>
_END;
    echo <<<_END
        <img src="coin.png" width="2%" alt="coins">
    _END;
    echo <<<_END
    <button onClick="more()">More!</button>//the button
    
    _END;
    
    ?>