I want to run multiple queries on node.js and mysql

Asked 2 years ago, Updated 2 years ago, 125 views

I have been trying to create a site using node.js and javascript while searching various ways.
I'm trying to write a code to send the results of multiple queries to ejs, but I get an error.
If you do results[0].length, results[1].length, and results[2].length in front of res.render, the correct size will be displayed, so you probably got it.
https://stackoverflow.com/questions/42974598/unexpected-token-return-in-while-compiling-ejs
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Errors/Malformed_formal_parameter
The above questions were a hit, but I don't use include.
I also tried using ejs-lint.

/home/ubuntu/views/results.ejs
  1:1 error Parsing error: Unexpected token <

✖ 1 problem (1 error, 0 warnings)

However, the < of the first character in the first line is an html tag, so it is hard to expect an error to occur there.
I apologize for the rudimentary question.I'd like to use your wisdom.
Thank you for your cooperation.

//server.js
const express=require('express');
const app=express();
const bodyParser=require('body-parser');
app.use(bodyParser.urlencoded({extended:true});

let http=require('http');
letfs=require('fs');
let mysql=require('mysql');

const connection=mysql.createConnection({
  multipleStatements: true,
  host: 'localhost',
  user: 'Username',
  password: 'Password',
  database: 'Database Name'
});

connection.connect(function(err){
  if(err)throwerr;
  console.log('Connected');
});

app.use(express.static('public'));
app.get('/',(req,res)=>{
  res.render('index.ejs');
  
});

app.post("/result",(req,res)=>{
  let selected = req.body.selected_series;
  let series;
  let human_series, item_series, quotation_series;
  // Put query into variable
  if(selected==="one"){
    series="SELECT* FROM human where series="+1+";SELECT* FROM item WHERE series="+1+";"; 
  } else if(selected==="two") {
    series="SELECT* FROM human where series="+2+";SELECT* FROM item WHERE series="+2+";SELECT* FROM quotation WHERE series"+";;
    
  } else if(selected==="nt") {
    series="SELECT* FROM human where series="+3+";SELECT* FROM item WHERE series="+3+";SELECT* FROM quotation series="+3+";;
  } else {
    human_series = Math.floor(Math.random()*4);
    item_series = Math.floor(Math.random()*4);
    quotation_series = Math.floor(Math.random()*4);
    series="SELECT* FROM human where series="+human_series+";SELECT* from item where series="+item_series+";";SELECT* from quotation where series="+quotation_series+";";
  }
  connection.query(
    series,
      (error, results) = > {
      res.render('results.ejs', {humans:results[0], items:results[1],quotations:results[2]});
      } 
    );
  });


app.listen(3000,()=>{
  console.log("My app listening on port 3000!");
});

results.ejs

for viewing
<html>
  <head>
    <title>Results</title>
  </head>
  <body>
    <%Object.values(humans);%>
    <%Object.values(items);%>
    <%Object.values(quotations);%>
    <p>------- Characters -----/p>
    <%humans.forEach(human)=>{%>
      <%=human%>
    <%}%>
    <p>-----item-----/p>
    <%items.forEach(item)=>{%>
      <%=item%>
    <%}%>
    <p>----- One word-----/p>
    <%quotations.forEach(quotation)=>{%>
      <%=quotation%>
    <%}%>
    <a href="/">Tell your fortune again</a>
  </body>
</html>

Error Contents

SyntaxError: Malformed arrow function parameter list in/home/ubuntu/views/results.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you want to create an async function, pass `async: true `as an option.
    at new Function (<anonymous>)
    at Template.compile (/home/ubuntu/node_modules/ejs/lib/ejs.js:662:12)
    at Object.compile (/home/ubuntu/node_modules/ejs/lib/ejs.js:396:16)
    athandleCache (/home/ubuntu/node_modules/ejs/lib/ejs.js:233:18)
    tryHandleCache (/home/ubuntu/node_modules/ejs/lib/ejs.js:272:16)
    at View.exports.renderFile [as engine] (/home/ubuntu/node_modules/ejs/lib/ejs.js:489:10)
    at View.render (/home/ubuntu/node_modules/express/lib/view.js:135:8)
    atryRender (/home/ubuntu/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/ubuntu/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/home/ubuntu/node_modules/express/lib/response.js: 1012:7)

Additional
I have given the value to server.js, so I will also paste index.ejs.

<html>
  <head>
    <metacharset="utf8">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script type="text/javascript" src="siren.js"></script>
    <link rel="stylesheet" href="style.css" type="text/css">
    <title>Title>
  </head>
  <body>
    <p>Please select a series</p>
    <form action="/result" method="POST">
      <select id="select_series" name="selected_series">
        <option value="none" name="series">--- Select --</option>
        <option value="one" name="series">SIREN</option>
        <option value="two" name="series">SIREN2</option>
        <option value="nt" name="series">SIREN:NT</option>
        <option value="all" name="series"> All Series</option>
      </select>
      <button type="submit" id="select">Determination</button>
    </form>
    <divid="result"></div>
  </body>
</html>

mysql node.js

2022-09-30 19:55

1 Answers

Thank you.
It was a rudimentary mistake to write forEach differently.

<html>
  <head>
    <title>Results</title>
  </head>
  <body>
    <%Object.values(humans);%>
    <%Object.values(items);%>
    <%Object.values(quotations);%>
    <p>------- Characters -----/p>
    <%humans.forEach(human=>{%>
      <%=human%>
    <%});%>
    <p>-----item-----/p>
    <%items.forEach(item=>{%>
      <%=item%>
    <%});%>
    <p>----- One word-----/p>
    <%quotations.forEach(quotation=>{%>
      <%=quotation%>
    <%});%>
    <a href="/">Tell your fortune again</a>
  </body>
</html>


2022-09-30 19:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.