我正在使用express, node, pg-promise, and ejs.
创建一个简单的待办事项应用程序我创建了一个数据库,但我无法在浏览器中显示内容。
奇怪的是,当我检查浏览器时,它会识别每个任务的形状,但除了<p>
标签外,它们都是空的。 <h1>
标签和内容显示出来,所以我认为这是一个比目鱼/鱿鱼问题,但我不太确定。
json数据出现了,但在切换到response.render并链接index.ejs文件后,我没有运气。
以下是文件结构的基本概述:
1. models
- task.js
2. views
- edit.ejs
- index.ejs
- new.ejs
- show.ejs
3. server.ejs, package.json, form.html
数据库名称:todo_app
表名:任务
id | subject | content
----+----------+---------------------------------
1 | planning | plot world domination
2 | garden | find victims for venus fly trap
3 | food | buy pickles
4 | postman | test post
注意:我在Postman中添加了第4号。
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
};
答案 0 :(得分:1)
我认为task.ejs
代表index.ejs
。<body>
<h1>Here's the task list:</h1>
<% tasks.forEach((everyTask) => {%>
<p>
<%= everyTask.content %>
</p>
<%})%>
</body>
。你的forEach应该如下:
window.SomeDeleteRowFunction = function SomeDeleteRowFunction(o) {
var p = o.parentNode.parentNode;
p.parentNode.removeChild(p);
$('#tbl tr').each(function(i) {
$(this).find('input').eq(0).attr("placeholder", i+1);
});
}
$('document').ready(function() {
$('.add_another').click(function() {
$("#tbl").append('<tr><td><input type="text" class="txtbox" value="" placeholder="' + ($('#tbl tr').length + 1) + '"/></td><td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)" /></td></tr>');
});
});