Results Returns Index Array is undefined

I have a question that I use Axios and Cheerio to get other web data about and take it into an array. Here I have an array

linkURLG = []

When console.log(linkURLG) it still releases the results but when I use it with
console.log(linkURLG.length) then it returns to 0 even though the array has 21 length
Where do I ask the problem and how to solve it? Thanks for reading.

const axios = require('axios')
const cheerio = require('cheerio')
const fs = require('fs')


let linkURLG = []
async function getData() {
    try {
            const url = 'https://viblo.asia/newest'
            const { data } = await axios({
            method: 'GET',
            url: url,
            // headers: {
            //     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36'
            // }
        })
        const $ = cheerio.load(data)
        const elementUrl = $('.post-feed-item')
        const dataGet = []
        const linkUrls = []
        $(elementUrl).each((index, element) => {
            const author = $(element).find('span.el-popover__reference-wrapper').text().trim()
            const authorAvt = $(element).find('a.d-flex.mr-05 > img').attr('src')
            const title = $(element).find('h3.word-break.mr-05').text()
            const url = 'https://viblo.asia' + $(element).find('h3.word-break.mr-05 > a').attr('href')
            const view = $(element).find('div.stats > span').first().text().trim()
            dataGet[index] = {author, authorAvt, title, url, view}
            linkUrls[index] = {url}
            linkURLG[index] = {url}
        })
        // console.log(dataGet)
        // console.log(linkUrls)
        document.getElementById('renderer-contain').innerHTML = dataGet.map(dataGet => `
    
        <div class="card">
            <div class="card-avt">
                <div class="card-avt-img"><img src="${dataGet.authorAvt}"></div>
            </div>
            <div class="card-contain">
                <div class="author"><span>Author</span> ${dataGet.author}</div>
                <div class="card-contain-title"><a href="${dataGet.url}">${dataGet.title}</a></div>
                <div class="view"><span>Lượt xem:</span> ${dataGet.view}</div>
            </div>
            
        </div>
        
        `).join('')

    } catch (error) {
        console.log(error)
    }
}
getData()

console.log(linkURLG)

async function getContent() {
    try {
        const url = 'https://viblo.asia/newest'
            const { data } = await axios({
            method: 'GET',
            url: url,
            // headers: {
            //     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36'
            // }
        })
        const $ = cheerio.load(data)

    } catch (error) {
        console.log(error)
    }
}
/* styles.css */

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

/* Add styles here to customize the appearance of your app */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: rgb(19, 19, 19);
    margin: auto;
    font-family: 'Roboto', sans-serif;
}

#renderer-contain {
    display: flex;
    gap: 5px;
    flex-direction: column;
    margin: 10px 10px;
}

.card {
    margin: auto;
    width: 90%;
    /* color: white; */
    background-color: white;
    border-radius: 5px;
    padding: 10px;
    display: flex;
    flex-direction: row;
    gap: 10px;
}

.card-avt {
    display: flex;
    justify-content: center;
    align-items: center;
}

.card-avt-img > img {
    width: 60px;
    border-radius: 5px;
}

.card-contain {
    display: flex;
    gap: 5px;
    flex-direction: column;
}

.author > span {
    background-color: rgba(0, 255, 255, 0.329);
    padding: 0px 5px 0px 5px;
    border: .5px solid rgba(0, 255, 255, 0.329);
    color: rgba(2, 91, 91, 0.566);
    border-radius: 5px;
}
.author {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 2px;
}


.card-contain .view {
    font-size: 12px;
}

.card-contain-title > a:link {
    text-decoration: none;
    font-weight: bold;
    color: rgb(71, 71, 255);
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'"> -->
    <link href="./styles.css" rel="stylesheet">
    <title>getApi</title>
  </head>
  <body>
    <div id="renderer-contain"></div>
    <!-- You can also require other files to run in this process -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="./renderer.js"></script>
  </body>
</html>

Substitution of function names and parameters for function output in JSON-like objects

Sometimes it’s useful to store function names and parameters in a JSON-like object structure, to allow inclusion of values in that vary, or are otherwise not known until run-time. For example, AWS Cloudformation allows functions prefixed with fn:: to be included, such as fn::select, an example of which might be:

const sourceObject = {
  "fruit": { "Fn::Select" : [ "1", [ "apples", "grapes", "oranges", "mangoes" ] ] },
  "somethingElse": [
    {"Fn::Join" : [ " ", [ "a", "b", "c" ] ]},
    "some value"
  ]
}

const substituted = substituteFunctionsInObject(sourceObject) 
// {"fruit": "grapes", "somethingElse":["a b c", "some value"]}

This form of substitution gives a good balance between giving expressive freedom, allowing data to be stored and passed around in pure JSON, and preventing arbitrary code injection. (See for example EVO’s answer to How to store a javascript function in JSON for another example, which uses “$” as a function tag instead of “fn::”)

Can anybody suggest a good version of the function that does such a substitution (substituteFunctionsInObject in the example above)? Ideally one in an existing library.

Such a function should allow specification of arbitrary functions to substitute, and should work recursively (deeply), perhaps allowing function arguments to be generated by additional function calls.

I’ve written my own implementation based on lodash’s transform function, and allowing additional data to be supplied via curried arguments. This works, but I can’t help thinking this problem must have been solved already in a more rigorous way.

const {transform} = require('lodash')   // fp.transform is broken
const fp = require('lodash/fp')

const applyFunctionsInObject = ({
  validFunctions = {
    "date": x=>new Date(x),
    "get": fp.get
  },
  getFunctionTag = fnName => "$" + fnName
}={}) => function applyFunctionsInObjectInner (obj){
  return (...data) => transform(    
    obj,
    (acc, item, key) => {
      const firstKey = Object.keys(item)?.[0];
      const functionIndex = Object.keys(validFunctions).map(getFunctionTag).indexOf(firstKey);
      const newItem = (fp.isPlainObject(item) || fp.isArray(item) ) 
        // only allow where one key, which is valid function name
        ? (firstKey && Object.keys(item).length===1 && functionIndex >-1 )  
          // call the function 
          ? Object.values(validFunctions)[functionIndex](     
            // allow substitution on arguments
            ...applyFunctionsInObjectInner(fp.castArray(item[firstKey]))(...data),
            // optional data 
            ...fp.castArray(data)       
          )  
          // recurse objects (deepness)
          : applyFunctionsInObjectInner(item)(...data) 
        : item ;        // pass non-object/array values directly
      acc[key]=newItem;
    }
  ) 
}

Javascript add integers to mixed array

I’m trying to create some synthetic data which needs to be represented in this kind of structure (an array of arrays):

dataset = [
  ['Period', 'c1', 'c2', 'c3'],
  ['2018-01', 2000, 2002, 2004],
  ['2018-08', 3000, 3003, 3006],
  ['2019-01', 4000, 4002, 4008]
]

I’ve made a function to help:

function get_datapoints() {
    dates = ['2018-01', '2018-08', '2019-01']

    data = [];
    data.push(['Period', 'c1', 'c2', 'c3'])

    for (i in dates) {
        sequence = []
        for (val = 1; val < 4; val++) {
            sequence.push(get_randomval())
        }
        data.push([dates[i], sequence.join()])
        console.log(data)
    }
    return data

function get_randomval(min = 2000, max = 5000) {
    let difference = max - min;
    let rand = Math.random();
    rand = Math.floor(rand * difference);
    rand = rand + min;
    return parseInt(rand);
}

This is generating data which looks like this:

[
  ['Period', 'c1', 'c2', 'c3']
  ['2018-01', '59797,47895,63209']
  ['2018-08', '28342,38450,70694']
  ['2019-01', '32348,44872,92501']
]

This understandably gives the error:
Error: Row 1 has 2 columns, but must have 4

Clearly there are two ‘fields’ in each data array, each enclosed in a single quote pair. I’m pulling my hair out trying to figure out how to get those integers into the array without the single quotes. This data is actually to be used as part of a google.visualization.arrayToDataTable needed by Google Charts.

https://developers.google.com/chart/interactive/docs/gallery/linechart
shows an example of this kind of structure (an array with both strings and ints).

How can I get those values into their arrays without the single quotes? Thanks!

Create rows and append them 4 elements of an object inside an array

I have this array:

let filtered = 
0: {name: 'Rick Sanchez', status: 'Alive', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/1.jpeg'}
1: {name: 'Morty Smith', status: 'Alive', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/2.jpeg'}
2: {name: 'Summer Smith', status: 'Alive', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/3.jpeg'}
3: {name: 'Beth Smith', status: 'Alive', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/4.jpeg'}
4: {name: 'Jerry Smith', status: 'Alive', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/5.jpeg'}
5: {name: 'Abadango Cluster Princess', status: 'Alive', species: 'Alien', image: 'https://rickandmortyapi.com/api/character/avatar/6.jpeg'}
6: {name: 'Abradolf Lincler', status: 'unknown', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/7.jpeg'}
7: {name: 'Adjudicator Rick', status: 'Dead', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/8.jpeg'}
8: {name: 'Agency Director', status: 'Dead', species: 'Human', image: 'https://rickandmortyapi.com/api/character/avatar/9.jpeg'}

I have more elements inside the array, but I want to put the info in cards (I’m using bootsrap 5 btw) but each 4 cards have to be inside a row and then I have to create a new row to put append them the next 4 cards

With map I can load the characters card but, how to append then each 4 cards in a new row??
Basically the structure should be

div.row
  div.col
  div.col
  div.col
  div.col
div.row
  div.col
  div.col
  div.col
  div.col

Should I use a for loop?? It could work but I’m not sure if it is the best solution, with .map I have no idea how to properly make a row each 4 elements

Im using only vanilla js

Problem catching errors in javascript function

I’m working on a web app with express that uses postgres as the backed database and trying to catch possible errors during the loading of data in the database but i have problems catching errors generated in the pool.connect function. I followed the code example in this page but i get a “Cannot set headers after they are sent to the client” when there is an error within the function. How can i solve it?

I investigated the problem and i think that it is related to the fact that some code is still executed even after one of the “return res.redirect(“/edit/upload”);” commands is called but at this point i don’t know the best approach to catch all the errors within the function.

module.exports.uploadDocument = async (req, res) => {
let data = [{user_code: req.body.code, document_date: req.body.date, document_type: req.body.doctype, company: req.body.company}]
const csv = new ObjectsToCsv(data);
let document_name = "document_".concat(req.session.passport.user, "_", req.body.identifier, ".csv");
let postings_name = "postings_".concat(req.session.passport.user, "_", req.body.identifier, ".csv");
csv.toDisk("./public/uploads/".concat(document_name));

await pool.query("create temp table temp_document (user_code integer not null, document_date date not null, document_type varchar(4) not null, company varchar(4) not null); create temp table temp_postings (gl_code varchar(10) not null, position integer not null, amount numeric(20, 4) not null, cost_center varchar(4), reg_date date not null, acc_center varchar(4), eff_date date not null, order_num integer, discount numeric(5,4), pay_mode varchar(4), suppl_code varchar(4), location varchar(4), country_code varchar(4), document_num integer);")

pool.connect(function (err, client, done) {
    var stream = client.query(copyFrom("coy temp_postings(gl_code, position, amount, cost_center, reg_date, acc_center, eff_date, order_num, discount, pay_mode, suppl_code, location, country_code) FROM STDIN WITH (FORMAT CSV, HEADER, DELIMITER ';')"));
    var fileStream = fs.createReadStream(`./public/uploads/${postings_name}`);
    fileStream.on('error', () => {
        req.flash("error", "Error with copy");
        return res.redirect("/edit/upload");
    })
    stream.on('error', () => {
        req.flash("error", "Error with copy");
        return res.redirect("/edit/upload");
    })
    fileStream.pipe(stream);
    var stream = client.query(copyFrom("copy temp_document(user_code, document_date, document_type, company) FROM STDIN WITH (FORMAT CSV, HEADER, DELIMITER ',')"));
    var fileStream = fs.createReadStream(`./public/uploads/${document_name}`);
    fileStream.on('error', () => {
        req.flash("error", "Error with copy");
        return res.redirect("/edit/upload");
    })
    stream.on('error', () => {
        req.flash("error", "Error with copy");
        return res.redirect("/edit/upload");
    })
    fileStream.pipe(stream);
    client.query("call data_loading();")
    .then (() => {
        fs.unlinkSync(`./public/uploads/${postings_name}`);
        fs.unlinkSync(`./public/uploads/${document_name}`);
        res.redirect("/");
    })
})

}

How to make a cookie, that is random, and won’t change once the user comes back (can’t be PHPSESSID= because im running node.js)

I’m trying to create a cookie that acts like a PHPSESSID but is JavaScript, I would just use PHP but i’m running node so that is impossible. The hope is that you get a 7 long character id that stays even when you reload the page and can’t be replicated. This is what I have tryed but it creates a new one instead of keeping it every time.

 function randomString() {  
            //define a variable consisting alphabets in small and capital letter  
    var characters = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";  
              
            //specify the length for the new string  
    var lenString = 7;  
    var randomstring = '';  
  
            //loop to select a new character in each iteration  
    for (var i=0; i<lenString; i++) {  
        var rnum = Math.floor(Math.random() * characters.length);  
        randomstring += characters.substring(rnum, rnum+1);  
    }  
  var thing = randomstring;
    document.cookie = `${thing};expires=4000;`
}  

How can i display name of uploaded file on navbar?

I have a problem with displaying name of the uploaded file on navbar. When i select file i want to upload in file browser/selector, i want my app to display it’s name on navbar.(Navbar is a separated component, but displayed on every page i have). Here is my code:

FileUploader:

import React from "react";
import { useDropzone } from "react-dropzone";
import Upload from "./pictures/fileUpload.png";

function Uploading(props) {
  const { getRootProps, getInputProps, acceptedFiles } = useDropzone({
    noDrag: true,
  });
  const files = acceptedFiles.map((file) => (
    <li key={file.path}>{file.name}</li>
  ));

  return (
    <section className="container w-full h-full text-center">
      <div {...getRootProps({ className: "dropzone h-full" })}>
        <input {...getInputProps()} />
        <img
          src={Upload}
          className="mx-auto cursor-pointer "
          alt=""
          id="picture"
        />
        <label class="text-2xl">Choose Document</label>
      </div>

    {/* This part of the code displays name of uploaded files in a list. */}
      <aside>
        <h4>Files</h4>
        <ul>{files}</ul>
      </aside>
      {/* End of displaying name code. */}
    </section>
  );
}

export default Uploading;

and Navbar:

import React from "react";
import Loaded from "../assets/loaded.png";

function Navbar() {
  return (
    // ->-> Absolute keeps navbar on one place, navbar doesn't follow the page with the mouse scrolling. <-<- //
    <div className="shadow-md w-full absolute">
      <div className="flex justify-end h-full w-full bg-white py-5 border-2 border-b-black">
        <div class="mr-5">
          <ul className="sm:w-fit md:w-fit lg:w-fit">
            <li className="text-xl">
              <img src={Loaded} className="h-8 w-8  cursor-pointer" alt="" />
            </li>
          </ul>
        </div>
      </div>
    </div>
  );
}

export default Navbar;

Testing JSON key and then writing nested values to existing JSON file

I have a changes.json file which has directives as key value pairs i.e. "action": "add" that I wish to test and depending on that test to then read in the payload to an array and update a source.json file. First here is the format of the original source JSON file. There are three sections to test, users, playlists, and songs.

{
    "users" : [
      {
        "id" : "1",
        "name" : "Albin Jaye"
      }
    ],
    "playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      }
    ],
    "songs": [
      {
        "id" : "1",
        "artist": "Camila Cabello",
        "title": "Never Be the Same"
      }
    ]
  }

And then the changes.json file that has the action directive to tell the code how to process, i.e. update, delete, add, etc

{
    "users": [{
        "action": "add",
        "payload": [{
                "id": "1",
                "name": "Albin Jaye"
            },
            {
                "id": "2",
                "name": "Dave Mustaine"
            }
        ]
    }],
    "playlist": [{
        "action": "add",
        "payload": [{
            "id": "1",
            "owner_id": "2",
            "song_ids": [
                "8",
                "32"
            ]
        }]
    }],
    "songs": [{
        "action": "add",
        "payload": [{
            "id": "1",
            "artist": "Camila Cabello",
            "title": "Never Be the Same"
        }]
    }]
}

Here is the code I have so far. First section I am able to iterate over the JSON and test the key by a switch:

Object.entries(data).map(([key, [{ action, payload }]]) => { 
    console.log(key, [{action, payload}]);
                    
    switch (key) {
        case 'users':
        // How to test for "add":"action"???
        // How to read in nested "payload"
        // How to update source JSON file with this value?
        break;
                        
    }
});

And for one of the tests/part, I have this code that reads through key/value pairs and pushes to array, but not sure about a couple of things.

const array = [];

    for (const [key, value] of Object.entries(data)) {
        if (data[key] === 'payload') {
            array.push([`${key}`, `${value}`]);
        }
    }
    console.log(array)
    // after reading into array, how to update source.json file with new data?
  1. How to test for the "action":"add" key/value once I test for "user" node. This is probably just a syntax thing? data[user.action]?

  2. Once tested, how to take the payload key/values and munge/update them back into the source.json`` file? I am guessing JSON.stringify(array) => fs.write(target)“` or something similar?

Thanks everyone!

Issue with .graphql files when switching project to nestjs

I had a project at which backend was written with GraphQL/lambdas. Now I want to convert whole project to NestJS/GraphQL.

The problem is as follows:

I have joined.graphql file in src/GraphQL folder and there is an import (usage) of this file in another .ts file. When I try to start the project with nest start command, none of *.graphql files are copied to dist folder. Then, that import throws an error as there is no joined.graphql file in the dist folder.

I tried to add nest-cli.json file with assets field to include ./**/*.graphql files as assets. According to the nestjs docs, now these files should also be copied. But they are not.

How can I proceed in this situation?

Why is the return keyword not giving me the desired result?

There is this challenge that I’d love to overcome. I’ve been stuck for almost 2 weeks. Here is the code:

function multiply(top,bottom){
  var num = top;
  var multiple = 2;
  while (num <= bottom){
    num *= multiple;
    console.log(num);
  }
  return num;
}

console.log(multiply(5, 100));// expected outcome is 80 

I’d love to return the last result from the console.log which is 80 but each time I use the return keyword, it doesn’t bring the desired result.

How to store socket on localStorage on vanilla ReactJS for use in different components socket.io?

I want to store the socket so I can use it in different components.
When I first set the socket on socket.io after login, I set the socket as a state under “socket” state and then store it on localStorage.

useEffect(() => {
        if(!socket) {
            setSocket(io(backendLink));
        };
    }, []) 

useEffect(() => {
        socket?.emit("newUser", localStorage.getItem('authToken'));
        localStorage.setItem('socket', JSON.stringify(socket));
    }, [socket])

Then in different components, where I did not initially set the socket. I use logic like this:

useEffect(() => {
        if(!socket) socket = localStorage.getItem('socket');
        socket.emit(...)
}, [userID])

Above, if the socket passed as props that was stored in state is null, i restore the socket from localStorage and use it to emit. However, I get Uncaught TypeError: Converting circular structure to JSON.

From what I understood, this is because I used JSON.stringify on something that cannot be converted and stored on localStorage this way i.e socket. I want to use vanilla reactjs to use the socket across as different files from the file i used setSocket(io(backendLink)). How to use socket.io across different components with vanilla reactJS(no context, completely vanilla)?

Password Validations using regex

function ValidatePassword() {

    var rules = [{
            Pattern: "[a-zA-Z]",
            Target: "UpperCase"
        },

        {
            Pattern: "[0-9]",
            Target: "Numbers"
        },
        {
            Pattern: "[!@@#$%^&*]",
            Target: "Symbols"
        }
    ];

    var password = $("#password").val();
    
    $("#Length").removeClass(password.length > 6 ? "fa-times" : "fa-check");
    $("#Length").addClass(password.length > 6 ? "fa-check" : "fa-times");
    for (var i = 0; i < rules.length; i++) {
        $("#" + rules[i].Target).removeClass(new RegExp(rules[i].Pattern).test(password) ?
            "fa-times" :
            "fa-check");
        $("#" +
            rules[i].Target).addClass(new RegExp(rules[i].Pattern).test(password) ? "fa-check" :
            "fa-times");
    }
} 
$(document).ready(function () {
    $("#password").on('onkeypress', ValidatePassword)

});

Validations not work according to rules and pattern ….. and i am using the onkrypress and these validations shows when page is reload..

$.templates is not a function in sails js chat

My Code
function renderChatMessages(data) { const chatContent = $(‘#chat-content’); const template = $.templates(‘#chatTemplate’); let htmlOutput = template.render(data); chatContent.append(htmlOutput); const scrollHeight = chatContent.prop(“scrollHeight”); chatContent.animate({ scrollTop: scrollHeight }, “slow”); }