[英]Embedded JS (ejs) isn't rendering data to browser
I'm working on a simple To Do app using express, node, pg-promise, and ejs.
我正在使用express, node, pg-promise, and ejs.
开发一个简单的To Do应用程序express, node, pg-promise, and ejs.
I created a database and I can't get the contents to show up in the browser. 我创建了一个数据库,但无法使内容显示在浏览器中。
The weird thing is that when I inspect the browser it recognizes the shape of each task, but they're empty except for the <p>
tags. 奇怪的是,当我检查浏览器时,它可以识别每个任务的形状,但是除了<p>
标记之外,它们都是空的。 The <h1>
tags and content shows up so I thought it was a flounder/squid issue, but I'm not so sure. <h1>
标签和内容显示出来,所以我认为这是一个比目鱼/乌贼的问题,但我不太确定。
The json data showed up, but after switching to response.render and linking the index.ejs file I haven't had any luck. 出现了json数据,但是切换到response.render并链接index.ejs文件后,我没有任何运气。
Here's a basic overview of the file structure: 这是文件结构的基本概述:
1. models
- task.js
2. views
- edit.ejs
- index.ejs
- new.ejs
- show.ejs
3. server.ejs, package.json, form.html
Database name: todo_app 数据库名称:todo_app
Table name: tasks 表名称:任务
id | subject | content
----+----------+---------------------------------
1 | planning | plot world domination
2 | garden | find victims for venus fly trap
3 | food | buy pickles
4 | postman | test post
NOTE: Number 4 I added with Postman. 注意:我与邮递员一起添加的4号。
index.ejs index.ejs
<body>
<h1>Here's the task list:</h1>
<% tasks.forEach((everyTask) => {%>
<p>
<%= tasks.content %>
</p>
<%})%>
</body>
</html>
server.js server.js
const express = require('express');
const app = express();
const PORT = 3000;
const bodyParser = require('body-parser');
// const models = require('./models/task')
app.use(bodyParser.json())
const urlencodedParser = bodyParser.urlencoded({ extended: false })
app.set("view engine", "ejs");
const findAllTasks = require('./models/task').findAllTasks;
const findById = require('./models/task').findById;
const createTask = require('./models/task').createTask;
// const edit = require('./models/task').edit;
// const delete = require('./models/task').delete;
app.get('/', (request, response) => {
findAllTasks().then(everyTask => {
response.render('index', { tasks: everyTask });
});
});
app.listen(PORT, () => {
console.log(`Welcome to the Year ${PORT}, the world of tomorrow!`)
});
task.js task.js
const pgp = require('pg-promise')({});
const connectionURL = "postgres://localhost:5432/todo_app";
const db = pgp(connectionURL);
const findAllTasks = () => {
return db.any('SELECT * FROM tasks');
}
const findById = id => {
return db.one('SELECT * FROM tasks WHERE id = $1', [id]);
};
const createTask = data => {
return db.one('INSERT INTO tasks(subject, content) VALUES($[subject], $[content]) RETURNING id', data)
}
module.exports = {
findAllTasks: findAllTasks,
findById: findById,
createTask: createTask
// edit: edit,
// delete: delete
};
I think with task.ejs
you mean: index.ejs
. 我认为使用task.ejs
意味着: index.ejs
。 and your forEach should be like the following: 并且您的forEach应该如下所示:
<body>
<h1>Here's the task list:</h1>
<% tasks.forEach((everyTask) => {%>
<p>
<%= everyTask.content %>
</p>
<%})%>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.