I'm working on a simple To Do app using 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. The <h1>
tags and content shows up so I thought it was a flounder/squid issue, but I'm not so sure.
The json data showed up, but after switching to response.render and linking the index.ejs file I haven't had any luck.
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
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.
index.ejs
<body>
<h1>Here's the task list:</h1>
<% tasks.forEach((everyTask) => {%>
<p>
<%= tasks.content %>
</p>
<%})%>
</body>
</html>
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
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
. and your forEach should be like the following:
<body>
<h1>Here's the task list:</h1>
<% tasks.forEach((everyTask) => {%>
<p>
<%= everyTask.content %>
</p>
<%})%>
</body>