Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I delete an html list item from Redis database using Jade template?
    primarykey
    data
    text
    <p>I have a very simple ToDo List app made (following tutorial in comment below) using node.js, express and jade. The app works so I can add items to the list and it saves to the redis database with the id equal to the name of the list item. </p> <p>Now how can I edit my jade template so a delete button shows up beside the list item, and when I click it, then item will be deleted from the database? </p> <p>Here is the relevant code:</p> <p>Routes.js file</p> <pre><code>exports.index = function(req, res){ res.render('index', { title: 'Welcome to JSPlayground Todo' }); }; var redis = require("redis"), client = redis.createClient(); exports.todo = function(req, res){ //Pulls items from database to display on page var todos = []; client.hgetall("Todo", function(err, objs) { for(var k in objs) { var newTodo = { text: objs[k] }; todos.push(newTodo); } res.render('todo', { title: 'New Todo List', todos: todos }); }); }; exports.saveTodo = function(req, res) { //Saves a list item from input box to database var newTodo = {}; newTodo.name = req.body['todo-text']; newTodo.id = newTodo.name.replace(/ /g, ''); //console.log(newTodo.id); //console.log(newTodo.name); client.hset("Todo", newTodo.id, newTodo.name); res.redirect("back"); }; </code></pre> <p>todo.jade file</p> <pre><code>h1 new todo list form(action="/save", method="post") p label Enter a new todo item input(type='text',placeholder='new todo', name='todo-text') p input(type='submit', value='Save') ul each todo in todos li #{todo.text} </code></pre> <p>If I make a link on the template page </p> <pre><code>a(href="/del") del </code></pre> <p>and route /del to this function in the routes.js file</p> <pre><code>exports.delTodo = function(req, res) { var delTodo = {}; delTodo.id = "Test"; client.hdel("Todo", delTodo.id); res.redirect("back"); }; </code></pre> <p>It will delete a single list item with id of "Test". But I am not sure how to get it to delete the specific list item that was clicked. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload