Function returns a pending promise, but value of variable is returned into it

This is the code

function readNotes(){
  return new Promise((resolve, reject) => {
     user = firebase.auth().currentUser.uid
     ref = database.ref("users/" + user + "/notes")
     ref.get().then( function(snapshot) {
         if (snapshot.exists()) {
              keys= Object.keys(snapshot.val())
              console.log(keys)
             resolve(keys);
         } else {
             console.log("No data available");
         }
     }).catch((error) => {
         reject(error);
     })
  })
}type here

I tried calling the function in the console and I was expecting for it to return a fulfilled promise, especially because I was able to log the value of the keys variable

React with Rails 7: Could not resolve “../components/EditTopic”

I’m very used to Rails but am adding React as a front-end for the first time and am hitting what I hope are very obvious beginner issues. I’m following this tutorial (roughly…with my own model structure), but it doesn’t actually go over adding edit routes. (It’s more CRD than CRUD for some reason…)

I have the following routes set up in my javascript/routes/index.jsx:

import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import MainNavbar from "../components/MainNavbar";
import Home from "../components/Home";
import Topics from "../components/Topics";
import Topic from "../components/Topic";
import NewTopic from "../components/NewTopic";


export default (
  <Router>
    <MainNavbar />
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/topics" element={<Topics />} />
      <Route path="/topic/:id" element={<Topic />} />
      <Route path="/topic" element={<NewTopic />} />
    </Routes>
  </Router>
);

With this structure, it works perfectly fine. However, when I add a route for EditTopic it disappears from the page and a server error pops up:

19:50:16 js.1 | [watch] build started (change:
“app/javascript/routes/index.jsx”) 19:50:16 js.1 | ✘ [ERROR] Could
not resolve “../components/EditTopic” 19:50:16 js.1 | 19:50:16 js.1
| app/javascript/routes/index.jsx:8:22: 19:50:16 js.1 | 8
│ import EditTopic from “../components/EditTopic”;

The routes I am adding are pretty simple. I use:

import EditTopic from "../components/EditTopic";

And for the actual route I have tried both:

  <Route path="/topics/:id" element={<EditTopic />} />

And:

  <Route path="/topics/:id" element={<EditTopic />} />

Both create the same error. Can anyone help out as to where I’m going wrong?

How to runJavaScript function only after contents loaded from JavaScript are fully loaded?

I have simple menu API and I have used JavaScript to load menu on menu.html file.

Menu item is like this:

<article class="item">
    <div class="img-container">
        <img width="300" height="300" src="img_URL" alt="img_name + photo"/>
    </div>
    <div class="info">
        <header>
            <span class="title">Item Name</span>
            
        </header>
        <div class="description">
            ItemDescription
        </div>
        <div class="bottom">
            <p class="price">$price</p>
            <div style="display:none" class="cart">
                <span class="add-to-cart"><i class="fas fa-cart-plus"></i></span>
                <div class="controller">
                    <p class="plus">+</p>
                    <p class="item-count" >0</p>
                    <p class="minus">-</p>
                </div>
            </div>
        </div>
    </div>
</article>

Now I want to add a add to cart functionality.
For this I would have to add event listener on plus and minus icon.
But since these contents are loaded from Javascript itself I get an empty array when I run
document.querySelectorAll(".plus")

How can I handle this?

How to change wordpress php code inside a HREF with javascript

I created two link fields in wordpress using Advanced Custom Fields.

enter image description here

their slug are:
“btn_um_assinar_link” for the default one and “btn_um_assinar_link_dois” for the second one

I set the first link in HTML with PHP via HREF:

<a id="btn-teste-gratis" href="<?php the_sub_field('btn_um_assinar_link'); ?>" target="_blank">DEFAULT LINK BUTTON</a>

But I want that when I click on the toggle button, the PHP code inside the HREF changes to another PHP code to get the second wordpress link and viceverse.

I’m trying to do like this inside javascript:

btnTesteGratis[0].href = "<?php the_sub_field('btn_um_assinar_link_dois'); ?>";

but the result is this:enter image description here

How can I get it right?

Using Regular Expression to validate the following requirement?

i am trying to validate a number exactly 15 digits with 5 digits group separated by “-” , “:” , “/”, that should start with either 5 or 4 or 37, it also should not contain any characters.

It means

37344-37747-27744 – valid
40344-37747-27744 – valid
59344-37747-27744 – valid
37344:37747:27744 – valid
37344/37747/27744 – valid
87887-98734-83422 – Not valid
548aa:aa764:90887 – not valid
377592993892093 – not valid

I was only able to go this far


d{5}[-:/]d{5}[-:/]d{5}

Please help.

Download a page only with the link? [duplicate]

I want to know is there any way to download a page only with the link.

For example : link and at the end we got the source code.

Here is my pseudo code :

/* 1- create a variable and save the link of the current page in it 2- download the saved link source code in a HTML file */

How can I see if 2 specific string variables added

How can I check if 2 variables with the same value are the same variable?

For example in the below code I created 2 variables with the same values and i pass:

function check_var(v){
  // Check if "v" (string) came from a or b
}

let a = "string";
let b = "string";

check_var(b);

I able to install v8 engine locally and change the the source code, but i am looking for more easy way to do it.

One way to do it is if I had a method in javascript that returns a unique value for each variable based on the variable name.

One more way to solve the problem is by logging all the javascript actions, by doing that i could see after the code executed if var a is the var that got into the check_var function.

Hosting HTML UTF-8 Problems

My HTML PAGE has utf-8 meta tags, why am I having a foreign character problem?

Problems : başladığım bu yazılım hayatında kendi düşünceme bağlı

META TAG :

Is this issue because Hosting doesn’t have a language pack or is there a bug in the browser or code?

RegEx find words start Capital letter end colon

I’m new to RegEx and JavaScript and I was wondering if anyone knew what the RegEx would be for detecting word start Capital letter end colon:

**Example string:

> const myStr=   'Title: Consectetur adipiscg elit  Description: Sed sit
> amet eros diam Details: Vestibulum a eros ut massa interdum
> consectetur  Article number: AYHMNR';

Result:

Title,Description,Details, Article number

I used:

/[:]/i and b[A-Z(...)].*?b here

How can i make this discord.js v14 slash command not return “undefined” for each option?

The idea of this command is to add a user to the .txt file full of customers info so it can be used by other parts of the bot.

The problem is no matter what i put into the options that pop up here

Command Example

it will always return “undefined” for each option like so.

Example Reply

i think the issue is its not actually returning anything from the interaction when asking for that information inside the execute function. im not quite sure why though.

here is the code in question.

const { SlashCommandBuilder } = require('discord.js');
const fs = require('fs');

module.exports = {
  data: new SlashCommandBuilder()
    // command name
    .setName('addfrozencustomer')
    // command description
    .setDescription('Add a frozen customer account/discord')
    .addStringOption(option =>
        option.setName('id')
            .setDescription('The ID of the customer')
            .setRequired(true))
    .addStringOption(option =>
        option.setName('username')
            .setDescription('The username of the customer')
            .setRequired(true))
    .addStringOption(option =>
        option.setName('email')
            .setDescription('The email of the customer')
            .setRequired(true)),
    async execute(interaction) {
        const { id, username, email } = interaction.options;
        const data = `${id}:::${username}:::${email}`;
        
        fs.appendFile('users/frozencustomers.txt', `${data}n`, (error) => {
            if (error) {
            console.error(error);
            return;
            }
        
            console.log('The data was successfully written to the file.');
        });
        interaction.channel.send(`Successfully added frozen customer:n ID: ${id}n User: ${username}n Email: ${email}`);
        }
              
};

Out of everything i have tried the code above is the closest iv come to getting this to work as intended.

HTML – detect click in empty select

I have an empty select. I need code to do something when the user clicks in the empty box. (I am using jQuery.)

<select id="my-select" style="height: 200px; width: 300px;"></select>

$('#my-select').click(() => {
    debugger;
});

Thanks!

Javascript every() display different value when doing a return and console.log()

I’m trying to solve a simple algorithm where you evaluate that every number in an array is greater than the next one, so I first taught in the Every() method, but when I do a return is always false, but when I console.log() it display true!

How is this possible?

const arr = [3, 2, 1];

const returnRes = arr.every((value, index, arr) => value > arr[index + 1]);

const consoleLogRes = arr.every((value, index, arr) => console.log(value > arr[index + 1]));

console.log(returnRes);

Evidence

document.querySelectorAll() not returning expected results (in puppeteer) [duplicate]

Using puppeteer in nodejs and trying to extract links from a page.

  let response;
  try {
    response = await page.goto(url)
    const noOfLinks = await page.evaluate(_ => document.querySelectorAll('a[href$=".pgn"]'));
    console.log({ status: response.status(), noOfLinks})
    console.log({href: noOfLinks[0].getAttribute('href')})
  } catch (e) { 
    console.log(e.toString()) 
  }

However, noOfLinks.getAttribute() returns an error:
TypeError: noOfLinks[0].getAttribute is not a function

It’s obvious why from the previous log statement:

{
  status: 200,
  noOfLinks: {
    '0': {},
    '1': {},...

Each link looks like an empty object. If I run .querySelectorAll and .getAttribute in a browser console for that page, I get the expected return values.

Is there something I need to do differently in puppeteer?

Why don’t my created element apply the CSS style?

On my project I have a function which simply add a element on the body.

function addListItem() {
    item = document.createElement('div')
    item.classList.add('item')

    checkboxdiv = document.createElement('div')
    checkboxdiv.className = "check-box"

    check_box_path = document.createElement('label')
    check_box_path.className = "checkbox path"

    checkbox = document.createElement("input")
    checkbox.type = "checkbox"

    svg = document.createElementNS("M5,10.75 L8.5,14.25 L19.4,2.3 C18.8333333,1.43333333 18.0333333,1 17,1 L4,1 C2.35,1 1,2.35 1,4 L1,17 C1,18.65 2.35,20 4,20 L17,20 C18.65,20 20,18.65 20,17 L20,7.99769186", "svg")
    svg.setAttribute("viewBox", "0 0 21 21")

    path = document.createElementNS("M5,10.75 L8.5,14.25 L19.4,2.3 C18.8333333,1.43333333 18.0333333,1 17,1 L4,1 C2.35,1 1,2.35 1,4 L1,17 C1,18.65 2.35,20 4,20 L17,20 C18.65,20 20,18.65 20,17 L20,7.99769186", "path")
    path.setAttribute("d", "M5,10.75 L8.5,14.25 L19.4,2.3 C18.8333333,1.43333333 18.0333333,1 17,1 L4,1 C2.35,1 1,2.35 1,4 L1,17 C1,18.65 2.35,20 4,20 L17,20 C18.65,20 20,18.65 20,17 L20,7.99769186")

    background = document.createElement("div")
    background.className = "background" 

    h1 = document.createElement("h1")
    h1.textContent = "Insira seu texto aqui"

    //svg

    svg.appendChild(path)

    //label

    check_box_path.appendChild(checkbox)
    check_box_path.appendChild(svg)

    //check-box

    checkboxdiv.appendChild(check_box_path)

    //background
    background.appendChild(h1)

    //item

    item.appendChild(checkboxdiv)
    item.appendChild(background)


    list = document.getElementById("list")
    return list.appendChild(item)
}

This item has a checkbox which I just took a copy of the css from another website (I’m gonna let the credits after), but the item created by the function just doesn’t apply the style when it is checked.

Putting the item on HTML file, its works well how should work.

here the checkbox css https://codepen.io/aaroniker/pen/PowZbgb

Checking the inspector, I can see the created element just doesn’t apply the check style when its checked.