Nodejs: Cannot set headers after they are sent to the

I write this code with NodeJS:

const setOutput = (res, req, data) => {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify(data));
}

app.post('/getDb', async (req, res, next) => {
    if (!req.body.hasOwnProperty('key')) {
        setOutput(res, req, {
            error: true,
            message: 'You must send key'
        })
    }
    
    const db = openDb(req.body.key);
    
    if (!db) {
        setOutput(res, req, {
            error: true,
            message: 'Not found key'
        })
    }

    setOutput(res, req, {
        error: false,
        message: 'DB create successful',
        data: {
            min: 0.1
        }
    });
})

This code work fine, but I get this error:

(node:253965) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

How can I stop anything after call setOutput?

Note: I don’t want use switch or a lot of if else

How to center a caption and pop up text on an image in html and css

I’m currently trying to code a website for the first time, and have run into the problem that when I’m trying to caption an image in a gallery and add a pop up info slide that it isn’t centered under the image, but instead it is to the right of the image. The image should always have a caption, and when you scroll over the image a text box should go up, so that a description could be read. I’m not very sure how to fix the problem since I thought I had already centered the text. If anyone can help me I’ll be very grateful.

CSS

image with caption

image with caption and pop up description

HTML code

How to secure a server (database) for open-source code?

So Im programming a public chrome extension and I will be reading/writing on a database. The code of Chrome Extensions is visible to anybody that installs it. So how do I access the server without showing any passwords or giving the user the option to write something there theirselves what isnt supposed to be there? How do I secure my server for open-source code?

How can I get useMutation (apollo-client) response

I am trying to get a useMutation response and put it inside a state. Code:

const [createMessage] = useMutation(CREATE_MESSAGE, {
    onCompleted(data) {
     console.log(data.createMessage) //works fine
     setMessageId(data.createMessage.id)
    },
    variables: {
      message: {
        body,
        title,
      },
    },
  });

But for some reason setMessageId doesn’t work. The state is still initial. What can I do to get a response?

Javascript Iterate elements of a first array at the position of another second array

hello i have the following code

let names = ["josh","tony","daniel"];  
let arrayplaces = ["30", "60", "90"];

names.forEach((elem, indexed) => {
  const num2 = arrayplaces[indexed];
  console.log('The user ' + elem + ' iterated in place ' + num2);
});

What I’m looking for is to iterate through all the elements of the first array into the first element of the second array, ending with the first one and continuing until it goes through the entire second array.

Example of the expected output:

The user josh iterated in place 30
The user tony iterated in place 30
The user daniel iterated in place 30

The user josh iterated in place 60
The user tony iterated in place 60
The user daniel iterated in place 60

The user josh iterated in place 90
The user tony iterated in place 90
The user daniel iterated in place 90

Does anyone know how I can do this?

undefined from a callback function [duplicate]

I want to access the data output from this full back function, however it log undefined

let data = xhr.addEventListener("readystatechange", function () {
    if (this.readyState === this.DONE) {
          let response = JSON.parse(this.response);
          const arr = response.map(({ age, name }) => ({
            age : parseInt(age),
            name: name.toUpperCase()
        }));
     
    let data = [...new Set(arr)];
    } 
});

// output here

console.log(data) // undefined

What is the best/simplest way to create a grid that is draggable, droppable, & sortable for web development? (work-related)

I am trying to create a grid layout where I have widgets that I can drag/drop/sort widgets onto it. I am hoping to find a free and easy way to do so. I have included snippets of Azure DevOps’ grid layout where I can select a widget and drag it onto any square, and the other widgets adjust their location accordingly. This is the kind of thing I am going for.

MDBootstrap, JQuery-UI,and SortableJS seem to be the most popular options, but I am struggling to find examples of the grid layout with code snippets. Most YouTube videos I have seen show lists as their example, but I have not seen a proper grid layout yet. Also, it seems that I have to pay for MDBootstrap’s implementation of these features, which is what I am trying to avoid.

Any help would be much appreciated, thank you!

Initial Grid Layout

Grid Layout After Drag/Drop/Sort

How to keep Quasar tab panel’s current tab on page refresh

I’m working on a Vue.js 2 application, and I’m having difficulties handling page refresh/reload properly.
There are three issues:

  • Loss of current route
  • Loss of router params
  • Loss of selected tab on QTabPanel component

I managed to solve the second problem by saving the router params on a Vuex store, but I don’t know how to solve the other two. I thought of storing also the selected Quasar’s tab on Vuex, but it could be annoying, giving the fact that, on some components, I have multiple levels of nested tab panels.
I also don’t know how to make the application stay on the current route after refreshing (I’m using history mode).
Could someone please give me a hint?

show alert if checked checkbox with different attr in javascript

In my project, I need to check if the data-id-row values are equal to the data-id-row values of the checked table rows when the checkboxes are checked, return a value if it is equal, and a message if it is not equal. Show it.

this is my rows

<table>
<thead>
<tr>
<th>
<input type="checkbox" name="id[]">
</th>
<th>
first name
</th>
<th>
last name
</th>
</tr>
</thead>
<tbody>
<tr id="6">
<td><input type="checkbox"></td>
<td>hamid</td>
<td>hamidi</td>
</tr>
<tr id="7">
<td><input type="checkbox"></td>
<td>john</td>
<td>tyler</td>
</tr>
<tr id="8">
<td><input type="checkbox"></td>
<td>john</td>
<td>smith</td>
</tr>

How can I do that?

neo4j and neovis cannot visualize on Azure web service

I make a neo4j server on azure virtual machine.
I use neovis to develop my app.
enter image description here
It is working fine on localhost (pictured above)
but when published to Azure web service it doesn’t show up
I get the following error by web console
enter image description here

this is my neovis config. How can i solve this problem?

var config = {
  server_url: "bolt://xx.xxx.xxx.xx:7687",
  container_id: "viz",
  server_user: "neo4j",
  server_password: "password",
  arrows: true,
  labels: {
    "Troll": {
      "caption": "name",
      "title_properties": [
        "name"
      ]
    }
  },
  relationships: {
    "RETWEETS": {
      "caption": false
    }
  },
  initial_cypher: cypher_script
};
viz = new NeoVis.default(config);  
viz.render();
console.log(viz);

Router on vanilla JS for SPA that uses history api?

I need a vanilla js router for a spa site. Here on the github I found it seems like an ideal implementation, but there is one problem. This implementation, and everything that I found on the Internet, works well only with hash, through the history api that I need, they refuse to work, tell me what I’m doing wrong, or tell me another working implementation.

So I just take the Router class code, and set the routes I need:

const router = new Router({
  mode: 'history',
  root: '/'
});

router
  .add('', () => {
    console.log('deafult page');
  }).add('settings', () => {
    console.log('settings page');
  }).add(/task-list/, () => {
    console.log('task-list page');
  })

And only the console.log('deafult page') is executed, if I enter http://localhost:3000/settings in the line, then it will give me Cannot GET /settings
if i use click event

document.addEventListener('click',()=>router.navigate('settings'))

then the link will become http://localhost:3000/settings, but the code console.log('settings page'); will not work

How can i use NPM package and make it work?

I’m trying to use NPM packages on on my project.

  1. I open project in VSCode.
  2. open terminal, type “npm install rellax –save”.
  3. see “up to date, audited 28 packages in 3s
    2 packages are looking for funding
    run npm fund for details
    found 0 vulnerabilities”
    What should i do next to make it work in my project page? Should i specify some path or use some import?

How to target multiple arrays in jQuery?

I would like to target the first through fourth elements within multiple arrays. I know how to do so with javascript but what the equivalent be in jquery?

var diags = [
[0, 7, 14, 21],
[1, 8, 15, 22],
[2, 9, 16, 23],
[3, 8, 13, 18],
[4, 9, 14, 19],
[5, 10, 15, 20],
[6, 13, 20, 27],
[7, 14, 21, 28],
[8, 15, 22, 29],
[9, 14, 19, 24],
[10, 15, 20, 25],
[11, 16, 21, 26],
[12, 19, 26, 33],
[13, 20, 27, 34],
[14, 21, 28, 35],
[15, 20, 25, 30],
[16, 21, 26, 31],
[17, 22, 27, 32],
[18, 25, 32, 39],
[19, 26, 33, 40],
[20, 27, 34, 41],
[21, 26, 31, 36],
[22, 27, 32, 37],
[23, 28, 33, 38],

];

function diagnolWin() {
for (var j = 0; j < diags.length; j++) {
    var optionOne = slots[diags][j][0];
    var optionTwo = slots[diags][j][1];
    var optionThree = slots[diags][j][2];
    var optionFour = slots[diags][j][3];