How to match pattern with Id on them react router

I am new to react. I have a route with react router that takes :id as parameter, how do I match this using react-router-dom useLocation.

basically path can be posts or post/21
how do I match post/21 to render a component, I want to render a seperate component if I
am on the detail page

{path === 'post/:id' ? <PostDetail /> : <Posts /> }

but use location returns posts/1 which I can’t use to match the post detail

summary
Any path that matches: post/:id

passing JavaScript variable into a php for loop not working [duplicate]

I am trying to limit a PHP loop based on a variable passed from JavaScript into the for loop as follows, however it doesn’t seem to be working.

</script>
var res =  5;
</script>

<?php
   $xx = "<script>document.writeln(res);</script>";
   echo $xx. " the number echo's fine";
?>

<br>

<?php  
for ($i= 1; $i <= $xx; $i++) {
  echo "The number is: $xx <br>";
}
?>  

Help with a solution would be greatly appreciated and thanks in advance!
I am new to PHP.

Aran

how to make an audio pause on reactjs?

I wanted to know why my audio is not pausing, it plays the normal audio but when I click the pause button it does not pause

const [iconAudio, iconAudioState] = useState(true)
const audio = new Audio(urlmp3)

<div>
                    <div onClick={()=>
                        {
                            iconAudio == true ? audio.play() : audio.pause()
                            iconAudio == true ? iconAudioState(false) : iconAudioState(true) 
                        } 
                    }>
                        {
                            iconAudio == true ? 
                                    <PlayArroyOutlined />
                                : 
                                    <PauseOutlined />
                        }
                    </div>
                </div>

TypeError Cannot read properties of undefined (reading ‘related’)

I am trying to follow a tutorial from someone who created a full website using Adonisjs, almost everything was exactly like showed in the tutorial, exept that the frontend was a little different, but the results were the same.
Now I am trying to get the posts showed in the browser, but when I try I get this error:
TypeError Cannot read properties of undefined (reading ‘related’)
I tried to see if I had mistaken something, but even using the same code used on the tutorial. The error is the same. Here is my code:

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import State from 'App/Enums/States'
import Post from 'App/Models/Post'
import PostStoreValidator from 'App/Validators/PostStoreValidator'

export default class PostsController {
  public async index({ view, auth, params }: HttpContextContract) {
    const page = params.page ?? 1
    const posts = auth.user!.related('posts').query().paginate(page, 20)
    return view.render('studio/posts/index', { posts })
  }

  public async create({ view }: HttpContextContract) {
    return view.render('studio/posts/create', {})
  }

  public async store({ request, response }: HttpContextContract) {
    const data = await request.validate(PostStoreValidator)

    await Post.create({ stateId: State.PUBLIC, ...data })

    return response.redirect().toRoute('studio.posts.index')
  }
}

But there are a lot more, so I am letting my gthub repositore here, so you can take a look at the entire code. The error shows some typeerror, but I have already tried to copy and paste the same code as gave on the tutorial, but nothing changes…

I hope someone can see what can be causing this error because obviously I can’t.

How is the function in my eventlistener able to receive an event without using function parameters? [duplicate]

I’m working on a very simple game using vanilla JavaScript and the behavior I’m seeing right now goes against everything I’ve learned about with functions. I don’t understand why getInput can be called without the usual parenthesis that you’d use when calling functions. In fact, when I add parenthesis like this: document.addEventListener("keydown", getInput());, it doesn’t work at all.

So then, how am I able to receive and print the event key without it having been passed as a parameter?

document.addEventListener("keydown", getInput);

function getInput() {
    console.log(event.key);
}

Swiper JS Slider next/previous buttons aren’t working perfectly

Hi guys I’m having a bit of trouble using swiper js slides. This is the link of my website having swiper js slider:
https://nyside2.internet.dk/
As you can see in the start the swiper js slider is showing a few parts from the next slide. This is a very big concern for me. Because I want only one slider per view also when I click on the next or previous button I’m having half part of the previous slider and half part of the next slider at the same time. Again this isn’t what I want I want only one slide after clicking the next or previous button.
You can see the code below that I’m using:

<HTML>
<head>
<style>
HTML,body
{position: relative;
  height: 100%;
}
body{
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color:#000;
  margin: 0;
  padding: 0;
}
#Slider .swiper-container {
  width: auto;
  height: 100%;
}
#Slider .swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  /* Center slide text vertically */
  display: flex;
  justify-content: center;
  align-items: center;
}

/* reset list */
#Slider ul.swiper-wrapper{
  list-style-type: none;
  padding: 0px;
  margin: 0px;
}

#Slider h2{
  flex-basis: 100%;
}


#Slider .swiper-button-prev,
#Slider .swiper-button-next{
 top: 45%;
 background: none;
 color: #FFFFFF;
 opacity: 0;
}

#Slider .swiper-container:hover .swiper-button-prev{
    opacity: 1.0;
    animation-duration: 1s;
    animation-iteration-count: 1;
    margin-left: 30px;
    animation-name: PreviousSlider;
}

u/keyframes PreviousSlider{
    0%{
    opacity: 1;
    }
    100%{
    opacity: 1;
    transform: translate(30px, 0px);
    }
}


#Slider .swiper-container:hover .swiper-button-next{
    opacity: 1.0;
    animation-duration: 1s;
    animation-iteration-count: 1;
    margin-right: 30px;
    animation-name: NextSlider;
}


u/keyframes NextSlider{
  0%{
    opacity: 1;}
  100%{
    opacity: 1;
    transform: translate(-30px, 0px);}
}

#Slider .h2--small .heading-line--0{
    position: absolute;
    display: block;
    top: 50%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 60px;
    font-weight: 200;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .h2--small .heading-line--1{
    position: absolute;
    display: block;
    top: 58%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 60px;
    font-weight: 500;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
} 

#Slider .content_subtitle{
    position: absolute;
    display: block;
    top: 68%;
    margin-left: 20px;
    z-index: 9;
    font-family: futura-pt;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 579px;
    height: 87px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 29px;
    letter-spacing: 0px;
    font-weight: 400;
    font-size: 24px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .button--inline{
    position: absolute;
    display: block;
    top: 77%;
    margin-left: 20px;
    z-index: 8;
    background-color: rgba(0, 0, 0, 0);
    font-family: "News Cycle";
    text-transform: uppercase;
    height: auto;
    width: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: nowrap;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 23px;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 20px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
    border-width: 0px;
    border-radius: 0px;
}

#Slider .button--inline .b8{
    color: #EF8524;
}

#Slider .fa-long-arrow-alt-right:before{
    color: #EF8524;
    text-align: left;
    letter-spacing: 0px;
    font-weight: 400;
}

#Slider .video-overlay{
    margin-top: -759px;
    width: 100%;
    height: 750px;
    position: relative;
    display: flex;
    background: black;
    opacity: 0.5;
}

/* Extra small devices (phones, 600px and down) */
u/media only screen and (max-width: 600px) and (min-width: 360px){
#Slider .swiper-button-prev,
#Slider .swiper-button-next{
display: none;
}

#Slider .h2--small .heading-line--0{
    position: absolute;
    display: block;
    top: 22%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 25px;
    font-weight: 200;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}    

#Slider .h2--small .heading-line--1{
    position: absolute;
    display: block;
    top: 33%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 25px;
    font-weight: 500;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .content_subtitle{
    position: absolute;
    display: block;
    top: 53%;
    margin-left: 20px;
    z-index: 9;
    font-family: futura-pt;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 320px;
    height: 87px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 25px;
    letter-spacing: 0px;
    font-weight: 400;
    font-size: 18px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .button--inline{
    position: absolute;
    display: block;
    top: 84%;
    margin-left: 20px;
    z-index: 8;
    background-color: rgba(0, 0, 0, 0);
    font-family: "News Cycle";
    text-transform: uppercase;
    height: auto;
    width: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: nowrap;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 23px;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 18px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
    border-width: 0px;
    border-radius: 0px;
}
}


/***********


/* Small devices (portrait tablets and large phones, 600px and up) */
u/media only screen and (max-width: 768px) and (min-width: 600px){
#Slider .swiper-button-prev,
#Slider .swiper-button-next{
 top: 35%;
 background: none;
 color: #FFFFFF;
 opacity: 0;
}

#Slider .h2--small .heading-line--0{
    position: absolute;
    display: block;
    top: 40%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 50px;
    font-weight: 200;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .h2--small .heading-line--1{
    position: absolute;
    display: block;
    top: 53%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 50px;
    font-weight: 500;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
} 

#Slider .content_subtitle{
    position: absolute;
    display: block;
    top: 68%;
    margin-left: 20px;
    z-index: 9;
    font-family: futura-pt;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 579px;
    height: 87px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 29px;
    letter-spacing: 0px;
    font-weight: 400;
    font-size: 24px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .button--inline{
    position: absolute;
    display: block;
    top: 85%;
    margin-left: 20px;
    z-index: 8;
    background-color: rgba(0, 0, 0, 0);
    font-family: "News Cycle";
    text-transform: uppercase;
    height: auto;
    width: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: nowrap;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 23px;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 20px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
    border-width: 0px;
    border-radius: 0px;
}

#Slider .button--inline .b8{
    color: #EF8524;
}
}

/* For tablet devices 768px to 992px */
u/media only screen and (max-width: 992px) and (min-width: 768px){
#Slider .swiper-button-prev,
#Slider .swiper-button-next{
 top: 38%;
 background: none;
 color: #FFFFFF;
 opacity: 0;
}

#Slider .h2--small .heading-line--0{
    position: absolute;
    display: block;
    top: 45%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 60px;
    font-weight: 200;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .h2--small .heading-line--1{
    position: absolute;
    display: block;
    top: 58%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 60px;
    font-weight: 500;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
} 

#Slider .content_subtitle{
    position: absolute;
    display: block;
    top: 72%;
    margin-left: 20px;
    z-index: 9;
    font-family: futura-pt;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 579px;
    height: 87px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 29px;
    letter-spacing: 0px;
    font-weight: 400;
    font-size: 24px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

 .button--inline{
    position: absolute;
    display: block;
    top: 85%;
    margin-left: 20px;
    z-index: 8;
    background-color: rgba(0, 0, 0, 0);
    font-family: "News Cycle";
    text-transform: uppercase;
    height: auto;
    width: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: nowrap;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 23px;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 20px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
    border-width: 0px;
    border-radius: 0px;
}

.button--inline .b8{
    color: #EF8524;
}
}

/* Large devices (laptops/desktops, 992px and up) */
u/media only screen and (max-width: 1200px) and (min-width: 992px) {
#Slider .swiper-button-prev,
#Slider .swiper-button-next{
 top: 45%;
 background: none;
 color: #FFFFFF;
 opacity: 0;
}

#Slider .h2--small .heading-line--0{
    position: absolute;
    display: block;
    top: 50%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 60px;
    font-weight: 200;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .h2--small .heading-line--1{
    position: absolute;
    display: block;
    top: 58%;
    margin-left: 20px;
    z-index: 11;
    font-family: futura-pt;
    font-style: Regular;
    text-transform: uppercase;
    height: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 894px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 75px;
    letter-spacing: normal;
    font-size: 60px;
    font-weight: 500;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
} 

#Slider .content_subtitle{
    position: absolute;
    display: block;
    top: 68%;
    margin-left: 20px;
    z-index: 9;
    font-family: futura-pt;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: normal;
    width: 579px;
    height: 87px;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 29px;
    letter-spacing: 0px;
    font-weight: 400;
    font-size: 24px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
}

#Slider .button--inline{
    position: absolute;
    display: block;
    top: 77%;
    margin-left: 20px;
    z-index: 8;
    background-color: rgba(0, 0, 0, 0);
    font-family: "News Cycle";
    text-transform: uppercase;
    height: auto;
    width: auto;
    color: rgb(255, 255, 255);
    text-decoration: none;
    white-space: nowrap;
    min-height: 0px;
    min-width: 0px;
    max-height: none;
    max-width: none;
    text-align: left;
    line-height: 23px;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 20px;
    transform-origin: 50% 50%;
    opacity: 1;
    transform: translate(0px, 0px);
    visibility: visible;
    border-width: 0px;
    border-radius: 0px;
}

#Slider .button--inline .b8{
    color: #EF8524;
} 



</style>
</head>
<body>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swiper-bundle.min.css">
<!-- Slider main container -->
<div class="swiper-container">
  <!-- Additional required wrapper -->
  <ul class="swiper-wrapper">
    <!-- Slides -->
    <li class="swiper-slide">
    <div>
        <video 
source src="//nyside2.internet.dk/wp-content/uploads/2022/03/moviefront720.mp4" type="video/mp4" lazyload="//nyside2.internet.dk/wp-content/uploads/2022/03/moviefront720.mp4" ></video>
    <div class="video-overlay"> </div>
    <h2 class="h2 content-copy__title h2--small"><span class="heading-line heading-line--0">Forretningsorienteret</span>
    <span>
        <strong class="heading-line heading-line--1">
            bureau
        </strong>
    </span>
    </h2>
        <div class="b2 b2--spread content_subtitle">
            <p>
                Stærkt team med fokus på strategi og kreativitet der engagere og aktiver mennesker på deres kunderejse
            </p>
        </div>
    <div class="button hero-content-cta z4 button--inline">
            <a href="https://nyside2.internet.dk/?page_id=37">
                <span class="b8 z2">
                Se hvordan vi gør 
                </span>
                <i class="fas fa-long-arrow-alt-right">
                </i>
            </a>
        </div>
    </div>
    </li>
    <li class="swiper-slide">
      <div>
        <video source src="//nyside2.internet.dk/wp-content/uploads/2022/03/Frontmovie2_720.mp4" type="video/mp4" lazyload="//nyside2.internet.dk/wp-content/uploads/2022/03/Frontmovie2_720.mp4"></video>
        <div class="video-overlay"></div>
        <h2 class="h2 content-copy__title h2--small"><span class="heading-line heading-line--0">Fra kr. 2.980,- / mdr.</span>
        <span>
            <strong class="heading-line heading-line--1">
                Den fulde løsning
            </strong>
        </span>
        </h2>
        <div class="b2 b2--spread content_subtitle">
            <p>
                Nyt kreativt webdesign, der løfter din virksomhed og øger effekten af din indsats
            </p>
        </div>
    <div class="button hero-content-cta z4 button--inline">
            <a href="https://nyside2.internet.dk/about/">
                <span class="b8 z2">
                Interesseret - så klik 
                </span>
                <i class="fas fa-long-arrow-alt-right">
                </i>
            </a>
    </div>

      </div>
    </li>
  </ul>

  <!-- If we need navigation buttons -->
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://unpkg.com/[email protected]/swiper-bundle.min.js"></script>
<script>
var swiper = new Swiper('.swiper-container', {
  loop: true,
  slidesPerView: 1,
  centeredSlides: true,
  autoplay: {
    delay: 5000,
    autoplayDisableOnInteraction: false,  
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

 /* ON INIT AUTOPLAY THE FIRST VIDEO */
  on: {
    init: function () {
      console.log('swiper initialized');
      var currentVideo =  $("[data-swiper-slide-index=" + this.realIndex + "]").find("video");
      currentVideo.trigger('play');
    },
  },
});

/* GET ALL VIDEOS */
var sliderVideos = $(".swiper-slide video");

/* SWIPER API - Event will be fired after animation to other slide (next or previous) */
swiper.on('slideChange', function () {
  console.log('slide changed');
  /* stop all videos (currentTime buggy without this loop idea - no "real" previousIndex) */
  sliderVideos.each(function( index ) {
    this.currentTime = 0;
  });

  /* SWIPER GET CURRENT AND PREV SLIDE (AND VIDEO INSIDE) */
  var prevVideo =  $("[data-swiper-slide-index=" + this.previousIndex + "]").find("video");
  var currentVideo =  $("[data-swiper-slide-index=" + this.realIndex + "]").find("video");
  prevVideo.trigger('stop');
  currentVideo.trigger('play');
});
</script>
</body>
</html>

I am building a twitter clone with react.js. And I was adding icons by passing in the function but when i call it in the function the page goes blank

my app works fine when it’s like this:

function SidebarOption({ text, Icon }) {
  return (
    <div >
        <h2>{text}</h2>      
    </div>
  )
}

but when i add th <Icon/> like below the page goes blank

function SidebarOption({ text, Icon }) {
  return (
    <div >
        <h2>{text}</h2>
        <Icon /> {*when I add this the app page goes blank*}
    </div>
  )
} 

I have tried setting the “Homepage”:”.” but that didn’t work.

Can’t get cookie during _app initialization with getInitialProps

I’m using next-cookies to store auth credentials from user session, however, I can’t get them during app initialization -let’s say, the user refreshes the page or comes back later-, however, after app has been initialized -or user has loged in-, I get them navigating in the app.

This is important, because I want to fetch some initial data to be available in to the redux store from the beginning.

// pages/_app.js

import { useStore } from '../store/store';
import nextCookie from 'next-cookies';


    function MyApp({ Component, pageProps }) {
      const Layout = Component.layout || MainLayout;
      const store = useStore(pageProps.initialReduxState); // custom useStore method to init store
      const pageTitle = Component.title || pageProps.title || 'Título de la página';
    
      return (
        <Provider store={store}>
          <Layout pageTitle={pageTitle}>
            <Component {...pageProps} />
          </Layout>
        </Provider>
      );
    }
    
    export default MyApp;
    
    MyApp.getInitialProps = async (ctx) => {
      const { token, user } = nextCookie(ctx);
      /* TO DO
        the idea is to get cookie from the server side
        and pass it to the client side, if the cookie is
        active, initial data will be triggered from the
        initializers
      */
    
      return { pageProps: { token, user } };
    };

Check it out:

enter image description here

Is it a better way or native option to get the cookie without having to a different cookie dependency?

Btw, I already have a middleware that protects routes from non authenticated user which works fine:

// pages/_middleware.js

export function middleware(req) {
  const activeSession = req.headers.get('cookie');
  const url = req.nextUrl.clone();

  if (activeSession) {
    if (req.nextUrl.pathname === '/login') {
      url.pathname = '/';
      return NextResponse.redirect(url);
    }
    return NextResponse.next();
  }

  url.pathname = '/login';
  return NextResponse.rewrite(url);
}

Best way to debug javascript while there is no error message for reading non-existing field from an object

const ball = {
    x: canvas.width / 2,
    y: canvas.height / 2,
    size: 10,
    speed: 4,
    dx: 4,
    dy: -4
}

// draw ball on canvas 
function drawBall() {
    ctx.beginPath();
    ctx.arc(ball.x, ball.y, ball.xize, 0, Math.PI * 2);
    ctx.fillStyle = '#0095dd';
    ctx.fill();
    ctx.closePath();
}

I’m trying to draw a ball using canvas.

ctx.arc(ball.x, ball.y, ball.xize, 0, Math.PI * 2);

This line calls arc() and tries to read ball.size by access ball.xize. It’s a typo.
In languages I’m familiar with, typos like this result in error messages that can help quickly identify the cause. There is none with javascript. What is the canonical way to debug javascript in such cases? Do we have to read and verify each and every line like what I did here?

Why is ReactDOM.render() not rendering

I am trying to create a webpage with React however when I use ReactDOM.render(), it fails to render the element.Here is my HTML :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

    </head>
    <body>
        <div id="root"> </div>
        <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
        <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
        <script src ="ReactTest.js" type="text/babel">
        </script>
    </body>
</html>

Now here is my JavaScript code:

ReactDOM.render(
     <h1> Hello 1</h1>,
     document.getElementById("root")
)

Why does it fail to render?

Dragable div does not work in touch screen

I have a dragable div that works in computer very well but today I checked in touch screen and knew that it does not move !!! In my java script code I have onmousemove , is there any alternative for that in touchscreen? Can any body tell me what to do to solve this ? BTW I want this work in both touch screen and computer, not just in touch screen
My dragable div code is blow.

<div id="wrapper" style="border: 0.1vw solid gray;">
    <div id="mydiv">
        <img src="image/cs.png">
    </div>
</div>
<script>
dragElement(document.getElementById("mydiv"));

        function dragElement(elmnt) {
          var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
          if (document.getElementById(elmnt.id + "header")) {
            /* if present, the header is where you move the DIV from:*/
            document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
          } else {
            /* otherwise, move the DIV from anywhere inside the DIV:*/
            elmnt.onmousedown = dragMouseDown;
          }

          function dragMouseDown(e) {
            e = e || window.event;
            e.preventDefault();
            // get the mouse cursor position at startup:
            pos3 = e.clientX;
            pos4 = e.clientY;
            document.onmouseup = closeDragElement;
            // call a function whenever the cursor moves:
            document.onmousemove = elementDrag;
          }

          function elementDrag(e) {
            e = e || window.event;
            e.preventDefault();
            // calculate the new cursor position:
            pos1 = pos3 - e.clientX;
            pos2 = pos4 - e.clientY;
            pos3 = e.clientX;
            pos4 = e.clientY;
            
            let parentElement = elmnt.parentElement;
            if(elmnt.offsetTop < 0){elmnt.style.top = "0px"; return;}
            if(elmnt.offsetTop > (parentElement.offsetHeight - elmnt.offsetHeight)-2)     {
                elmnt.style.top = (parentElement.offsetHeight - elmnt.offsetHeight-2) + "px"; 
                return;
              }
            if(elmnt.offsetLeft < 0){elmnt.style.left = "0px";return}
            if(elmnt.offsetLeft > (parentElement.offsetWidth - elmnt.offsetWidth)-2){
                elmnt.style.left = (parentElement.offsetWidth - elmnt.offsetWidth-2) + "px";
                return;
            }
            
            // set the element's new position:
            elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
            elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
          }

          function closeDragElement() {
            /* stop moving when mouse button is released:*/
            document.onmouseup = null;
            document.onmousemove = null;
          }
        }
</script>

read/write JSON file in a simply web application with node.js

I have an HTML file (index.html) where I put a form and I want to see a list of elements that are inside a JSON file (elements.json).
I want to create a server with node.js, so I create app.js in this way:

const express = require('express')
const app = express()
const port = 3000
var config = require('./public/js/elements.json');

//static file
app.use(express.static('public'))
app.use('/css', express.static(__dirname+'public/css'))
app.use('/js', express.static(__dirname + 'public/js'))


//set view
app.set('views', './views')
app.set('view engine', 'ejs')

app.get('', (req, res) => {
    res.sendFile(__dirname+'/views/index.html')
})

//listen
app.listen(port, () => console.info(`Listening on port ${port}`) )

1. How can I do in my script.js file (that is in the same directory of elements.json) to show the elements in a table on my index.html?

2. When I fill the form in the HTML page, I want to add that element on my JSON file, in order to see it in my list of elements on HTML page. How can I do it?

Do While loop – Simply add numbers

I need to prompt the user to enter a series of numbers, or the word “quit”.
Then, If the user enters a number, add the new number to a running total.
And, If the user enters the word “quit” the loop should stop execution.
I cant figure what should I do here. Im a beginner. i don’t know how to make it work when user enter a word quit

let total = 0;
let number = 0;
    do {
        total += number;
        number = parseInt(prompt('Enter a number: '));
        if(number >= 0) {
            document.writeln("<p>You entered " + number + "!</p>");
        } else {
            if(isNaN(number))  {
            number = parseInt(prompt('Enter a number: '));
            document.writeln("<p>Try again. We are looking for a number!</p>");
            } 
        }
    } while(number >= 0)
    document.writeln("<p>The total is " + total + "</p>")