NodeJs with Redis Web App Part 4

Introduction

This is Part 4 of “NodeJs with Redis Web App,” a multiple-series tutorial. In Part 3, you learned how to configure search feature functionality. Specifically, how to show Redis values based on your search. Today, in Part 4, you’ll go over the steps on how to perform value deletions in Redis. Some of the examples included in this tutorial show you how to revise the handlebars template, search for a user to delete, use the override method, and test the Delete button you created. You’ll use Redis commands throughout this lesson.

If you are familiar with how NodeJs Redis Web App value deletions are done and want to skip the detailed steps, go to The Code.

Prerequisites

  • Read, practice, and review lessons in Parts 1, 2 and 3 of “NodeJs with Redis Web App,” the multiple-series tutorial.

Delete a Redis Document via ID using NodeJs

  • Put a button on the form by revising the details.handlebars template for your Redis application.
# {{user.firstname}} {{user.lastname}} [{{user.id}}]



- **Age:** {{user.age}}

- **Gender:** {{user.gender}}


[Back](https://intern.textbroker.com/)

de7ad15389aa44d1bd0524b1cfc72b9f.gif

  • The /user/delete/{{user.id}}?_method=DELETE must be appended on the action for the form before you use methodOverride.

NOTE: Your app.js is where you defined the -method.

  • Execute your NodeJs Redis Web App to verify that the button works. In a browser, go to the Localhost:3000 URL. For our example, search for usr101. The button should display. If it doesn’t, review the steps at the beginning of the “Delete a Redis Document via ID using NodeJs section.”

  • Make a user/delete for the route by opening and then appending the file app.js with the code here:

app.delete("/user/delete/:id", function(request, response) {
  rclient.del(req.params.id);
  response.redirect("/");
});
  • The code executes when it finds a URL that matches the search criteria. At that time, the parameter, which is the ID in this case, is shown in the URL, not within the form for your NodeJs Redis Web App.

  • Use the Redis command DEL to delete the req.params.id in the DEL key format.

  • When finished, the page will redirect to the Homepage.

  • Try out the route by running a user search as you did before. Next, press the command Redis Del to complete the deletion.

  • The result should be similar to this:

d3b1a42b7a48bb5c7d637a9921f84043.gif

  • After you delete a user, you won’t be able to find the record. When you attempt to, you should see a “Sorry that user does not exist” error message. That’s how you know the deletion was a success.

  • Another way to check if the record was deleted is to use the command prompt and start up redis-cli:

image shows all available Redis keys

According to the example, you were successful in deleting usr101 because it’s not in the list with the other users: usr102, user103, and user104.

Conclusion

This finishes Part 4 regarding the delete feature in NodeJs Redis Web App. In it, you learned how to use Redis commands to create a Delete button. This lesson showed you how to update the framework for handlebars, select a user to delete, code the override method, and tryout the Delete button.

Next, you searched for a user record to delete. After deleting a record, you saw that the “Sorry that user does not exist” prompt lets you know that your operation worked as planned. You also used the examples given in this tutorial to finish the steps for deleting values.

Deleting records is an integral part of application management. It’s good to know a simple way to code the deletion process for your NodeJs Redis Web Apps. Refer to this tutorial anytime for all of your NodeJs Redis projects.

The Code

The code details below are the entire sample steps for completing value deletions outlined in Part 4 of the multiple-series tutorial about NodeJs Redis Web App.

details.handlebars

# {{user.firstname}} {{user.lastname}} [{{user.id}}]



- **Age:** {{user.age}}

- **Gender:** {{user.gender}}


[Back](https://intern.textbroker.com/)

app.js

  • The code snippet of deleting a user
app.delete("/user/delete/:id", function(request, response) {
  rclient.del(request.params.id);
  response.redirect("/");
});

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.