-2
app.get('/all-datas', (req, res) => {

  data.find({}, function (err, names) {
    res.render('EJS.ejs', {
      nameList: Uname
    })

  })
})

I want this code to be used in my javaScript but when http://localhost:5000/all-datas is accessed this the page does not render and keeps loading and loading

or just tell me the any other way

I have installed all required modules tried changing the port done lots and lots changes still i cant help

  • 1
    Maybe EJS.ejs has an endless loop in it? – Wyck Dec 15 '22 at 02:02
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – jabaa Dec 15 '22 at 02:03
  • Welcome but voted to close because it is not clear what went wrong or any attempts you made to fix that also did not work. – Buzz Moschetti Dec 15 '22 at 02:19
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 16 '22 at 14:16

2 Answers2

1

I also had the same problem. The cause of the problem in my case was that I was not redirecting when handling post request. app.js

const express=require("express")
const bodyparser=require("body-parser")
const path=require("path")
const app=express()
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.use(bodyparser.urlencoded({extended:true}))
const list=[]
app.get("/", (req, res)=>{
    const day=new Date()
    const options={
        weekday:"long",
        day:"numeric",
        month:"long"
    }
    const today=day.toLocaleDateString("en-US", options)
    res.render("list.ejs", {day:today, list:list})
})
app.post("/", (req, res)=>{
    const task=req.body.newItem
    list.push(task)
    res.redirect("/")
})
app.listen(3000)

list.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1><%=day%></h1>
    <h2>To Do List</h2>
    <ul style="list-style-type: none; margin:0; padding: 0;">
        <%for(let i=0; i<list.length; i++){%>
            <li><%=list[i]%></li>
        <%}%>
    </ul>
    <form action="/" method="post">
        <input type="text" name="newItem">
        <input type="submit" value="Add">
    </form>
</body>
</html>
0

First of all, you don't need to specify the extension on '.ejs' on render. you are also not using the 'names" parameter inside the callback function. if you want to take 'Uname' from 'names'( Output docs of the query ), you can pass the whole array to ejs and take "uname" from there by using a loop (ex: forEach Loop).

To render all usernames form the Namelist

app.get('/all-datas', (req, res) => {

  data.find({}, function (err, names) {
    res.render('EJS', {
      nameList: names
    })
  })
})

on Ejs :

    <% nameList.ForEach((item)=>{ %>
    <p> username: <%= item.Uname %> </p>
   <%  }) %>

If This doesn't work, Make sure you have written the loop on the Ejs file correctly.

Roshith
  • 35
  • 6